我有下面的代码:jQuery用户界面自动完成功能按键和显示问题
function testSearch() {
$(".searchfield").autocomplete({
source: function (request, response) {
$.ajax({
url: "{{ path('my_search') }}",
dataType: "json",
data: {
term: request.term
},
success: function (data) {
response($.map(data, function (item) {
return {
label: item.label,
value: item.value
}
}));
}
});
},
minLength: 1,
select: function (event, ui) {
$.each(ui, function (key, value) {
window.location = "{{ path('my_show_product', {'productId': ''}) }}" + "/" + value
});
},
focus: function (event, ui) {
/* $.each(ui, function(key, value) {
alert(key + " " + value);
});*/
},
})
$.ui.autocomplete.prototype._renderItem = function (ul, item) {
var a = $('<a>', {
href: "{{ path('my_show_product', {'productId': ''}) }}" + "/" + item.value,
text: item.label
});
var $li = $('<li></li>', {
style: "width:100%"
});
return $li.append(a).data('item.autocomplete', item.value).appendTo(ul);
};
}
我的数据是从功能,由$.ajax
函数调用返回之上,在此JSON格式:
[ { label: 'test label', value: '1234' }, { label: 'test label1', value: '4567' } ]
输入栏的样子:
<input type="text" class="searchfield" name="searchfield" value="Search for Products" />
到目前为止,我可以搜索项目和g et结果以上述格式显示在前端的列表中。每个项目引用一个页面http://mydomain/products/(value)
目前,我得到以下问题:如果我搜索某物和搜索列表出现,我可以使用我的键盘浏览列表,点击输入按钮并重定向到正确的页面,但是我正在关注的每个项目的完整标签都不会显示在上面显示的输入搜索栏中。
我该如何做到这一点?
编辑: 所以我试图修改代码以下列:
function testSearch() {
$(".searchfield").autocomplete({
source: function(request, response) {
$.ajax({
url: "{{ path('my_search') }}",
dataType: "json",
data: {
term: request.term
},
success: function(data) {
response($.map(data, function(item) {
return {
label: item.label,
value: item.value
}
}));
}
});
},
minLength: 1,
select: function(event, ui) {
$.each(ui, function(key, value) {
window.location = "{{ path('my_show_product', {'productId': ''}) }}"+"/" + value
});
},
focus: function(event, ui){
$('.ui-autocomplete-input').val(ui.item.label);
$(this).val(ui.item.label);
//alert($('#searchfield')[0].className);
//alert(ui.item.label);
//$(this).val($(ui.item).text());
// $('.searchfield').val($(this).val(ui.item.label));
}
})
$.ui.autocomplete.prototype._renderItem = function(ul, item) {
var a = $('<a>', {
href: "{{ path('my_show_product', {'productId': ''}) }}"+"/" + item.value,
text: item.label
});
var $li = $('<li></li>', {style:"width:100%"});
return $li.append(a).data('item.autocomplete', item).appendTo(ul);
};
}
,但它似乎是响应决策问题:如果我离开:
response($.map(data, function(item) {
return {
label: item.label,
value: item.value
}
}));
比文本字段获得每个标签的“值”,这不是我想要的。如果我将其更改为:
response($.map(data, function(item) {
return {
label: item.label,
value: item.label
}
}));
我看到在矿井输入字段中的标签,但链接的每个项目是在结构http://mydomain/(label),而不是http://mydomain/(value)。什么我需要的是后者。
任何想法?
嗨,谢谢。问题是在焦点事件我可以访问标签..如果我写console.log(ui.item.label)它说未定义。如果我删除焦点事件,我可以使用按下按钮,但是我在“搜索字段”中没有显示任何内容 – ramo
是的,您可以访问标签。如果你想编写自己的逻辑来设置带有这个标签的文本框值,那么你应该这样做。如果您只是从选项中删除该设置,则插件将在文本框中显示突出显示的值。 – ShankarSangoli
是否可以,该插件的默认行为是“销毁”,当我做$ .ui.autocomplete.prototype._renderItem =功能(你的东西在我的代码?因为即使我删除焦点事件,我没有得到任何值显示在文本框中,它只是在我的第一个评论中留下空白 – ramo