我有一个webservice方法,它接受一个元素的id来确定自动复制的源。
简而言之,我这样做:
$("input[type='text']").autocomplete({
source: function(request, response) {
var id = $(this).attr('id');
var params = {'id': id, 'term': request.term};
var jsonParams = JSON.stringify(params);
$.ajax({
type: "POST",
url: "Page.aspx/GetAutoCompleteList",
data: jsonParams,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
response(JSON.parse(msg.d));
},
error: function() {
response([]);
}
});
}
});
但id
并未引用原始选择器。
如何获取所选input
元素的ID?或者对此更好的策略是什么?
答案 0 :(得分:2)
您需要维护每个输入元素的上下文,如下所示:
$("input[type='text']").each(function (i, ele) {
ele = $(ele);
ele.autocomplete({
source: function (request, response) {
var id = ele.attr('id');
var params = {'id': id, 'term': request.term};
var jsonParams = JSON.stringify(params);
$.ajax({
type: "POST",
url: "Page.aspx/GetAutoCompleteList",
data: jsonParams,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
// ...
},
error: function () {
// ...
},
async: false
});
}
});
});
答案 1 :(得分:1)
尝试
res {WorldCurrencies.CurrencyTypes} WorldCurrencies.CurrencyTypes
AED "United Arab Emirates Dirham" string
AFN "Afghan Afghani" string
ALL "Albanian Lek" string
AMD "Armenian Dram" string
ANG "Netherlands Antillean Guilder" string
AOA "Angolan Kwanza" string
ARS "Argentine Peso" string
AUD "Australian Dollar" string
AWG "Aruban Florin" string
AZN "Azerbaijani Manat" string
BAM "Bosnia-Herzegovina Convertible Mark" string
BBD "Barbadian Dollar" string
BDT "Bangladeshi Taka" string
BGN "Bulgarian Lev" string
...
$(this.element).prop("id");
this.element[0].id;
$(this.element.get(0)).attr('id');
$("input[type='text']").autocomplete({
source: function(request, response) {
var id = $(this.element).prop("id");
var id2=this.element[0].id;
var id3=$(this.element.get(0)).attr('id');
console.log(id);
console.log(id2);
console.log(id3);
var params = {'id': id, 'term': request.term};
var jsonParams = JSON.stringify(params);
$.ajax({
type: "POST",
url: "Page.aspx/GetAutoCompleteList",
data: jsonParams,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
response(JSON.parse(msg.d));
},
error: function() {
response([]);
}
});
}
});