如何返回显式"No results found"值以选择2 ajax搜索?



我使用的是select2jQuery下拉框插件(最新版本(,但存在一个问题,即我允许用户输入员工ID以返回匹配员工的姓名。这是有效的。

在所有搜索中,我都会返回一个值(ajax调用的"状态",如果找到,则返回employee数组(。我怀疑这可能会混淆select2,使其总是认为有结果。

当没有返回实际员工时,我如何明确告诉select2没有找到结果,以便它显示该消息?

目前,当没有找到结果时,我得到了一个严重的jQuery错误:

jquery-3.6.0.min.js:2 jQuery.Deferred exception: Cannot read properties of undefined (reading 'length') TypeError: Cannot read properties of undefined (reading 'length')
at e (https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.13/js/select2.min.js:2:39169)
at Object.<anonymous> (https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.13/js/select2.min.js:2:38243)
at e (https://code.jquery.com/jquery-3.6.0.min.js:2:30038)
at t (https://code.jquery.com/jquery-3.6.0.min.js:2:30340) undefined

我很快就找到了答案,我本来打算删除它,但我认为这可能会帮助未来的程序员。

简单地返回一个显式的";结果";带有空json对象的值,格式为select2,如下所示:

return { results: [] };

相关内容

最新更新