autocomplete :如何更改仅包含以 input-word 开头的元素的结果列表?
我有这样的代码:
<script type="text/javascript">
var myList = [ "Avellino", "Enna", "Frosinone" ];
$("#myTextBox").autocomplete({
source: myList,
appendTo: "#myOwnMenuProvince"
});
</script>
现在,如果我输入e
,我只想看到列表中以e
开头的单词,而不是那个包含e
。因此,在上面的示例中,只有 Enna
。
我该怎么做呢?
I have this code :
<script type="text/javascript">
var myList = [ "Avellino", "Enna", "Frosinone" ];
$("#myTextBox").autocomplete({
source: myList,
appendTo: "#myOwnMenuProvince"
});
</script>
Now, if I digit e
I'd like to see only words in the list that start with e
, not that contain e
. So, in the example above, only Enna
.
How can I do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
默认情况下,插件使用方法
$.ui.autocomplete.filter
。此方法使用正则表达式对数据的标签/值属性执行 grep 操作,并返回过滤后的数据。让我们从中得到启发来进行过滤。要使用您自己的过滤方法,请使用
source
选项作为回调。request
参数是一个包含一个属性term
的对象。它包含要搜索的文本,基本上是输入字段的值。因此,如果您在输入字段中键入“en”,request.term
将具有该值。response
参数是一个回调。你调用它,传递过滤后的数据,显示数据,基本上显示结果弹出菜单。所以基本上,您将过滤数据并将过滤后的数组传递给“响应”回调。
这是一种方法:
DEMO
By default, the plugin uses the method
$.ui.autocomplete.filter
. This method does a grep using a regular expression on the label/value properties of the data and returns the filtered data. Let's inspire ourselves from that to make our filtering.To use your own filtering method, use the
source
option as a callback.the
request
parameter is an object containing one propertyterm
. It contains the text to search for, basically the value of the input field. So if you type "en" in the input field,request.term
will have that value.the
response
parameter is a callback. You call it, passing the filtered data, to display the data, basically show the results popup menu.So basically, you would filter your data and pass the filtered array to the `response´ callback.
Here's one way to do it:
DEMO