1
我有問題在循環中添加自動完成插件多個文本輸入(沒有錯誤返回)。添加jQuery的自動完成準備好多個文本輸入
// get text inputs to attach autocomplete
var locinputs = $('#localizations').find('input:text');
// iterate over elements and add autocomplete plugin
for (var i = 0; i < locinputs.length; i++) {
// asp.mvc array with special characters replacing
var locNameField = locinputs[i].name.replace('[', '\\\\[');
locNameField = locNameField.replace(']', '\\\\]');
locNameField = locNameField.replace('.', '\\\\.');
$('input#' + locNameField).autocomplete('<%=Url.Action("GetCity", "Localization") %>', {
extraParams: {
provinceId: function() { return 21; }
},
dataType: 'json',
parse: function (data) {
var rows = new Array();
for (var i = 0; i < data.length; i++) {
rows[i] = { data: data[i], value: data[i].PlaceId, result: data[i].Name };
}
return rows;
},
formatItem: function (row, i, n) {
return row.Name;
},
width: 300,
mustMatch: true,
multiple: true
});
}
有上加載沒有錯誤,我嘗試使用Firebug的調試,元素是jQuery選擇
<input type="text" name="loc[0].CityNames" id="loc[0].CityNames" value="" />
更正聲明, $訪問。( '#本地化輸入:文本')各(函數(){
$(this).autocomplete('<%=Url.Action("GetCity", "Localization") %>', {
extraParams: {
provinceId: function() { return 21; }
},
dataType: 'json',
parse: function (data) {
var rows = new Array();
for (var i = 0; i < data.length; i++) {
rows[i] = { data: data[i], value: data[i].PlaceId, result: data[i].Name };
}
return rows;
},
formatItem: function (row, i, n) {
return row.Name;
},
width: 300,
mustMatch: true,
multiple: true
});
});
我將代碼移至每個jquery語句,id不起作用。但是,當我嘗試訪問元素 $(「input#loc \\\ [0 \\\] \\。CityNames」);它是可訪問的(從螢火蟲)。 – marcinn 2010-05-10 13:52:13
嘗試把警報插入到每個:它被稱爲正確的次數? 還有一件事:在每個函數中,「this」將引用DOM元素。因此,爲了與當前元素操縱嘗試使用 $(本).attr(「名稱」)等 – Juriy 2010-05-10 13:59:16
對jQuery和$(本)Juriy謝謝:) – marcinn 2010-05-10 14:14:03