我在下面的问题上需要你的帮助。
我正在尝试使用jQuery AutoComplete函数设置一个Input Hidden值,但是没有传递该值(ui.item.bank_id未定义)。
Javascript代码:
$("#reg_bank").autocomplete({
//define callback to format results
source: function(req, add){
//pass request to server
$.getJSON("/get_banks.php?callback=?", { q:req.term }, function(data) {
//create array for response objects
var suggestions = [];
//process response
$.each(data, function(i, val){
suggestions.push(val.bank_name+"("+val.bank_id+")");
});
//pass array to callback
add(suggestions);
});
},
select: function(e, ui) {
alert(ui.item.bank_id);
//$('#reg_bank_id').val(ui.item.bank_id);
},
change: function() {
}
});
出于某种原因,我无法弄明白,ui.item.bank_id is undefined
。
我的JSON输出类似的东西:
[{"bank_id":"341","bank_name":"Ita\u00fa Unibanco S.A."}]
如果您需要任何其他信息,请与我们联系..
THX ..
答案 0 :(得分:2)
我认为您应该调整源函数,如下所示
source: function(req, add){
//pass request to server
$.getJSON("/get_banks.php?callback=?", { q:req.term }, function(data) {
//create array for response objects
var suggestions = [];
//process response
$.each(data, function(i, val){
suggestions.push({'label':val.bank_name, 'value':val.bank_id});
});
//pass array to callback
add(suggestions);
// now suggestions is arrays of object with 'label' and 'value' properties
});
}
并选择功能如下
select: function(e, ui) {
alert(ui.item.value); // will get bank_id
alert(ui.item.label); // will get bank_name
}