Nik*_*kov 20 jquery jquery-autocomplete jquery-widgets
我有几个使用jQuery自动完成功能增强的输入字段.触发选择事件时如何获取相应的输入字段?
<input name="1" value="">
<input name="2" value="">
<input name="3" value="">
$('input[type=text]').autocomplete({
source: 'suggestions.php',
select: function(event, ui) {
// here I need the input element that have triggered the event
}
});
Run Code Online (Sandbox Code Playgroud)
Naz*_*nzo 31
我正在为source
param 使用匿名函数,并$(this)
在其中引用了函数,而不是触发它的元素.我不得不使用$(this.element)
.
最终的代码与此类似(我将其简化为演示目的):
$( ".regionsAutocomplete" ).autocomplete({
source: function(request, response){
//The next line is the important one for this example
var input = this.element;
$(input).css('color','red');
var data = {'foo':'bar'};
$.ajax({
'url': ajaxurl,
'data': data,
'method': "POST",
'dataType': "json",
'success': function(data) {
response(data);
}
});
}
});
Run Code Online (Sandbox Code Playgroud)
Adi*_*dil 12
尝试使用 $(this)
$('input[type=text]').autocomplete({
source: 'suggestions.php',
select: function(event, ui) {
// here I need the input element that have triggered the event
alert($(this).attr('name'));
}
});
Run Code Online (Sandbox Code Playgroud)