【发布时间】:2012-02-27 05:10:34
【问题描述】:
我正在研究自动完成并使用了这个http://jqueryui.com/demos/autocomplete/#combobox
现在我的查询是这个 api 让用户可以灵活地输入和搜索组合框的值,但由于这个用户可以输入任何内容。但是有没有办法限制用户提交错误的搜索查询。这是因为我的组合框将用作搜索条件,并且它的值被提交到下一页,并且由于此功能,它向页面提交了错误的数据。
提前谢谢你... 谢谢
【问题讨论】:
我正在研究自动完成并使用了这个http://jqueryui.com/demos/autocomplete/#combobox
现在我的查询是这个 api 让用户可以灵活地输入和搜索组合框的值,但由于这个用户可以输入任何内容。但是有没有办法限制用户提交错误的搜索查询。这是因为我的组合框将用作搜索条件,并且它的值被提交到下一页,并且由于此功能,它向页面提交了错误的数据。
提前谢谢你... 谢谢
【问题讨论】:
您可以尝试自定义匹配器功能来强制匹配或清除字段...
$("#input").autocomplete({ << initialise the autocomplete here >>})
.on('blur', function(event){
// Grab the autocomplete object
var autocomplete = $(this).data("autocomplete");
var matcher = new RegExp("^" + $.ui.autocomplete.escapeRegex($(this).val()) + "$", "i");
// Iterate through the autocomplete list items to find any partial or full matches
autocomplete.widget().children(".ui-menu-item").each(function() {
var item = $(this).data("item.autocomplete");
if (matcher.test(item.value || item)) {
//There was a match
matchcount++;
autocomplete.selectedItem = item;
return;
}
});
if (autocomplete.selectedItem) {
//if there was a match trigger the select event on that match
autocomplete._trigger("select", event, {
item: autocomplete.selectedItem
});
//there was no match, clear the input
} else {
$(this).val('');
}
});
【讨论】: