我无法弄清楚为什么在jquery自动完成时,select函数会弄乱数据。我的意思是value
= label
,当它不应该时。
console.log(val);
Object { value="1558825", label="Cree sus propias noticias cliente", icon="http://servidor...News/10_ae4e0.jpg"}
console.log(ui.item);
Object { label="Cree sus propias noticias cliente", value="Cree sus propias noticias cliente"}
代码:
$("#search_input").autocomplete({
source: function(req, add) {
$.getJSON("do.php", { OP: "news_search", category: cat_id, get: req }, function(results){
var suggestions = [];
$.each(results, function(i, val){
console.log(val);
suggestions.push(val.label)
});
add(suggestions);
});
},
select: function(event, ui){
console.log(ui.item); // Here value and label is the same, when it shouldn't
$("#search_input").val(ui.item.label).attr('data-target', ui.item.value);
return false;
},
minLength: 2
});
任何想法为什么会发生这种情况?
答案 0 :(得分:1)
代码suggestions.push(val.label)
仅推送标签。因此,除ui
对象
尝试推送val对象suggestions.push(val)