这里是新手编码员。我有一个带有 jQuery 自动完成功能的搜索栏,通过本地 json 数组进行搜索。当没有找到匹配项时,我想返回一个字符串“Nothing found”。
我已经尝试过 $.grep 中的 if 语句,但到目前为止没有任何效果:
$("#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);
}
}));
},
感谢您的帮助!! :)
我想你想测试“OR”(||
) here:
if (matcher.test(value.value).length && matcher.test(value.nickname).length == 0) {
return not_found;
}
仅当两者都为真时value.value
AND value.nickname
没有价值。 (你还没有显示你的数据,所以我在这里猜测)
if (matcher.test(value.value).length || matcher.test(value.nickname).length == 0) {
return not_found;
}
如果其中一个不为真,则匹配,如果满足则立即退出value.value
确实有长度。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)