Jquery UI 自动完成 - 显示首选的方式
我是一名新手程序员/设计师,试图配置 jquery ui 自动完成。我使用 javascript 对象(数组)让它工作。该数组包含我们引导客户前往的零售商店。我们有某些首选商店,因此我们希望它们比其他商店更早出现,但我真的不知道如何将它们过滤到结果中,或者是否可能。
如果有人输入“Widget”,我希望“Widget Store 4”首先出现。这是 jquery 代码:
var widgetstores = [
{label: "Widget Store 1", value: "1001" }, {label: "Widget Store 2", value: "1002" }, {label: "Widget Store 3", value: "1003" }, {label: "Widget Store 4", value: "1004" }, {label: "Widget Store 5", value: "1005" }, {label: "Widget Store 6", value: "1006" }
]
$(function() {
$('#tags').autocomplete({
minLength: 3,
source: widgetstores,
focus: function(event, ui) {
$('#tags').val(ui.item.label);
return false;
},
select: function(event, ui) {
$('#tags').val(ui.item.label);
$('#customer_num').val(ui.item.value);
return false;
}
})
.data( "autocomplete" )._renderItem = function( ul, item ) {
return $( "<li></li>" )
.data( "item.autocomplete", item )
.append( "<a>" + item.label + "</a>" )
.appendTo( ul );
};
});
I'm a newbie programmer / designer trying to configure jquery ui autocomplete. I have it working, using a javascript object (array). The array contains retailer stores that we are directing our customers to. We have certain stores that are preferred and so we would like for them to come up earlier than other stores but I don't really know how to filter them into the result or if it is even possible.
I want "Widget Store 4" to appear first if anyone types in "Widget". Here's the jquery code:
var widgetstores = [
{label: "Widget Store 1", value: "1001" }, {label: "Widget Store 2", value: "1002" }, {label: "Widget Store 3", value: "1003" }, {label: "Widget Store 4", value: "1004" }, {label: "Widget Store 5", value: "1005" }, {label: "Widget Store 6", value: "1006" }
]
$(function() {
$('#tags').autocomplete({
minLength: 3,
source: widgetstores,
focus: function(event, ui) {
$('#tags').val(ui.item.label);
return false;
},
select: function(event, ui) {
$('#tags').val(ui.item.label);
$('#customer_num').val(ui.item.value);
return false;
}
})
.data( "autocomplete" )._renderItem = function( ul, item ) {
return $( "<li></li>" )
.data( "item.autocomplete", item )
.append( "<a>" + item.label + "</a>" )
.appendTo( ul );
};
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定您使用的是什么 AutoComplete 库,但它看起来像:
http://docs.jquery .com/UI/Autocomplete
该库不会自动为您对项目进行排序。它使用传递给脚本的项目的顺序,并从中进行过滤。因此,如果您的项目在传递到自动完成之前按照您想要的顺序排列,那么它应该使用该顺序。
更新:
I'm not sure what AutoComplete library you're using, but it looks like:
http://docs.jquery.com/UI/Autocomplete
That library does not sort the items for you automatically. It uses the order of the items passed to the script, and filters from that. So if your items are in the order you want them before you pass to the autocomplete it should use that order.
Update: