你好,美好的社区!
我在jquery中遇到了访问函数中的$(this)的问题。通常,我会使用var来传递它,但在实际情况下,我不能定义任何var。
我不知道如何传递"source: function()“的参数。
我试过"source: function(请求、响应、self = $(this))“,但它不起作用。
$(".autocomplete-file-label").autocomplete({
minLength: 3,
source: function(request, response){
var category = $(this).attr("data-category");
if(category == '')
{
var source = 'http://www.example.com/?term=' + request.term;
}
else
{
var source = 'http://www.example.com/?term=' + request.term + '&type=' + category;
}
$.ajax({
url: source,
data : request.term,
dataType: "json",
type: "POST",
success: function (data)
{
response($.map(data, function( item ) {
return item;
}));
}
});
},
focus: function( event, ui ) {
$(this).val( ui.item.label );
return false;
},
select: function( event, ui ) {
$(this).val( ui.item.label);
$(this).next(".autocomplete-file-id").val( ui.item.id );
return false;
}
})如果你有任何解决办法,我会非常高兴的!
非常感谢!
发布于 2018-08-07 13:08:11
需要元素中特定实例数据的插件的一种常见方法是在each循环中启动插件。
$(".autocomplete-file-label").each(function() {
var category = $(this).attr("data-category");
$(this).autocomplete({
minLength: 3,
source: function(request, response) {
if (category == ''){
///.....
}
}
//....
}
});
});https://stackoverflow.com/questions/51727507
复制相似问题