我的json字符串如下所示:
[
{
"CustomerCountryId": "1",
"CountryName": "Australia"
},
{
"CustomerCountryId": "1",
"CountryName": "Austria"
},
{
"CustomerCountryId": "1",
"CountryName": "Belgium"
}
]
我想使用JQuery
自动完成功能将CountryName
设置为文本框,"CustomerCountryId"
当我输入国家/地区名称时,它不会在自动填充框中显示数据。
答案 0 :(得分:1)
您可以使用此方法:将您的数据转换为Array
label
,value
和id
属性,然后获取id
属性选择功能:
HTML代码:
<input id="search" />
<input type="text" id="CustomerCountryId" />
jQuery代码:
var data = [
{
"CustomerCountryId": "3",
"CountryName": "Australia"
},
{
"CustomerCountryId": "2",
"CountryName": "Austria"
},
{
"CustomerCountryId": "1",
"CountryName": "Belgium"
}
];
$("#search").autocomplete({
source: data.reduce(function(array, item){
array = array || [];
array.push({label: item.CountryName, value: item.CountryName, id: item.CustomerCountryId});
return array;
}, []),
select: function( event, ui ) {
$("#CustomerCountryId").val(ui.item.id);
}
});