jQuery UI自动完成:返回“找不到任何内容”没有搜索匹配时 [英] jQuery UI Autocomplete: return "nothing found" when no search matches occur
问题描述
新手编码器在这里。我有一个jQuery自动完成的搜索栏,搜索本地json数组。如果没有找到匹配项,我想返回一个字符串,上面写着找不到任何东西。
Newbie coder here. I have a search bar with jQuery autocomplete, searching through a local json array. When no matches are found, I want to return a string that says "Nothing found."
我在$ .grep中尝试了if语句但到目前为止没有任何工作:
I've tried if statements inside $.grep but nothing has worked so far:
$("#div_name").autocomplete({
appendTo: ".custom-autocomplete",
source: function (request, response) {
var matcher = new RegExp($.ui.autocomplete.escapeRegex(request.term), "i");
response($.grep(array, function(value) {
var not_found = 'Nothing found.';
if (matcher.test(value.value).length && matcher.test(value.nickname).length == 0) {
return not_found;
}
else {
return matcher.test(value.value)
|| matcher.test(value.nickname);
}
}));
},
感谢您的帮助!! :)
Thanks for your help!! :)
推荐答案
我想你想测试一下对于'OR'( ||
)这里:
I think you want to test for 'OR' (||
) here:
if (matcher.test(value.value).length && matcher.test(value.nickname).length == 0) {
return not_found;
}
只有在 value.value <时才会为真/ code> AND
value.nickname
没有值。 (你没有显示你的数据,所以我猜这里)
will only be true if both value.value
AND value.nickname
do not have a value. (You haven't shown your data, so I'm guessing here)
if (matcher.test(value.value).length || matcher.test(value.nickname).length == 0) {
return not_found;
}
如果不是真的话会匹配,如果<$ c $立即纾困c> value.value 确实有一个长度。
will match if either is not true, bailing out immediately if value.value
does have a length.
这篇关于jQuery UI自动完成:返回“找不到任何内容”没有搜索匹配时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!