我已经实现了按预期工作的自动完成文本框。

当前,如果在建议中找到“ No Match”,则显示“ No results”作为建议,但是当他们单击时,该值将应用于tex框

var error = ["No match"];
if (results.length == 0) {
    responseFn(error);
}
else {
    responseFn(results);
}


结果是将在autosuggest.s中显示的匹配项的列表。如果结果为空,则会建议说“ No Match”,但我不想让用户选择它,所以我尝试了以下代码,if (results.length == 0) {

$("#txtNewAttributes").blur(function () {
    $("#" + txtbxId).val("");
})


但这会清除所有情况下的文本框,即使找到匹配的项目也是如此。我该如何实施呢?

如果仅找不到匹配项,则需要清除TextBox。

最佳答案

我之前也面临过同样的问题。

在jquery.autocomplete中,有不同的回调函数,例如focus,change。

如果使用以下回调函数找不到匹配项,则可以清除文本框。

change: function (e, u) {
            //If the No match found" u.item will return null, clear the TextBox.
            if (u.item == null) {
                //Clear the AutoComplete TextBox.
                $(this).val("");
                return false;
            }
        }


此解决方案为我工作。

10-04 22:35
查看更多