2017-08-17 60 views
2

我正在使用jQueryUI自動完成的Web項目。我需要得到每個被稱爲inputname屬性。我怎麼才能得到它? this無法獲取函數內的上下文。我如何獲取jquery-ui自動填充小部件的上下文?

$("input").autocomplete({ 
    delay: 600, 
    minLength: 2, 
    source: function(request, response) { 
    var term = request.term; 
    $.getJSON(url, request, function(data, status, xhr) { 
     response(data); 
    }); 
    } 
}); 
+0

我有幾個輸入,所以我需要知道誰在觸發。 –

回答

3

您可以通過在each()循環內初始化自動完成來實現此目的。這將意味着您可以訪問this參考:

$("input").each(function() { 
    var $input = $(this); 

    $input.autocomplete({ 
    delay: 600, 
    minLength: 2, 
    source: function(request, response) { 
     var term = request.term; 
     // do something with $input.prop('name') here... 
     $.getJSON(url, request, function(data, status, xhr) { 
     response(data); 
     }); 
    } 
    }); 
}); 
相關問題