我正在從jQuery UI的自動完成組件。雖然我的自動完成工作正常,但如果我鍵入建議的字母「我」它顯示所有列表,這是可用的JSON數據,我只需要相關的信例如,如果我鍵入字母「印度」,印度尼西亞等。我只需要在列表中顯示最多10個值。在這裏我已更新最新代碼我正在嘗試在自動完成中執行切片,並且正在嘗試在下一個文本框中獲得相同的值。自動完成jQuery的建議不工作
這裏是當前jQuery代碼
$("#ipt_Countryres").autocomplete({
source: function(request, response) {
var regex = new RegExp(request.term, 'i');
//var filteredArray = filteredArray.slice(0,10);
$.ajax({
url: "json/countries.json",
dataType: "json",
data: {term: request.term},
success: function(data) {
response($.map(data, function(item) {
if(regex.test(item.label)){
return {
id: item.id,
label: item.label,
value: item.value
};
}
}));
},
minlength:2,
select: function (event, ui) {
$("#get_country").val(ui.item.label);
}
});
}
});
下面是HTML代碼
<input type="text" id="ipt_Countryres" name="ipt_Countryres" class="ipt_Field"/>
<input type="text" class="ipt_Field" id="get_country" name="get_country"/ disabled>
這裏是我的示例JSON數據
[
{
"value":"Afghanistan",
"label":"Afghanistan",
"id":"AF"
},
{
"value":"Åland Islands ",
"label":"Åland Islands",
"id":"AX"
},
{
"value":"Albania ",
"label":"Albania",
"id":"AL"
},
{
"value":"Algeria ",
"label":"Algeria",
"id":"DZ"
},
{
"value":"American Samoa ",
"label":"American Samoa",
"id":"AS"
},
{
"value":"AndorrA ",
"label":"AndorrA",
"id":"AD"
},
{
"value":"Angola ",
"label":"Angola",
"id":"AO"
},
{
"value":"Anguilla ",
"label":"Anguilla",
"id":"AI"
},
{
"value":"Antarctica ",
"label":"Antarctica",
"id":"AQ"
},
{
"value":"Antigua and Barbuda ",
"label":"Antigua and Barbuda",
"id":"AG"
}]
請請幫助我。
提前感謝
馬哈德
您可以來看看下面這樣: [http://stackoverflow.com/questions/3148195/jquery-ui-autocomplete-use-startswith][1 ] [1]:http:// stackoverflow。com/questions/3148195/jquery-ui-autocomplete-use-startswith – vijayP