如果您使用的是集合列表,则组合框选项效果很好,但是如果您有通过 json get 动态生成的列表,那么您只能捕获更改时的数据。
下面带有附加参数的完整示例。
$("#town").autocomplete(
{
select: function( event, ui ) {
$( "#town" ).val( ui.item.value );
return false;
},
focus: function( event, ui ) {
$( "#town" ).val( ui.item.label );
return false;
},
change: function(event, ui) {
if (!ui.item) {
$("#town").val("");
}
},
source: function(request, response) {
$.ajax({
url: 'urltoscript.php',
dataType: "json",
data: {
term : request.term,
country : $("#abox").val() // extra parameters
},
success: function(data) {
response($.map(data,function (item)
{
return {
id: item.id,
value: item.name
};
}));
}
});
},
minLength: 3
, highlightItem: true
});