bjo*_*k24 9 jquery jquery-ui jquery-ui-autocomplete
鉴于以下代码,如何从调用success()函数中引用具有自动完成功能的输入$.ajax?无论是$(this)或$e工作.
$('.parent-input').autocomplete({
source: function(request, response) {
$.ajax({
url: "/chunky/bacon",
dataType: 'json',
data: {
product_id: $('#product-id').val(),
term: request.term
},
success: function(data){
var resultCount = data.length;
// I NEED TO REFERENCE .parent-input HERE
response( data );
}
});
},
minLength: 2,
select: function(event, ui){
addAssociatedProduct(ui.item.id, ui.item.value);
$(this).val('');
return false;
}
});
Run Code Online (Sandbox Code Playgroud)
保存对this.element(this.element是一个jQuery对象的引用,所以实际上不需要将它包装在另一个jQuery调用中):
$('.parent-input').autocomplete({
source: function(request, response) {
var element = this.element; // <-- this.element is the input the widget is bound to.
$.ajax({
url: "/chunky/bacon",
dataType: 'json',
data: {
product_id: $('#product-id').val(),
term: request.term
},
success: function(data){
var resultCount = data.length;
// element still refers to the input the widget is bound on.
// for example:
element.addClass("blue");
response( data );
}
});
},
minLength: 2,
select: function(event, ui){
addAssociatedProduct(ui.item.id, ui.item.value);
$(this).val('');
return false;
}
});
Run Code Online (Sandbox Code Playgroud)