简体   繁体   中英

How do I reference the sepcific object/dom element that an jquery-ui autocomplete function belongs to, when it has been applied to all inputs

$("input[type=text]").autocomplete({
minLength: 3,
source: function (request, response) {
        alert( $(this).val() );

I have 3 different input text box's, but $(this).attr("id") or this.id both returned undefined

Not sure what is available inside source regarding this . You can always log it to console to see what is returned. The following pattern is helpful for implementing plugins on many elements

$("input[type=text]").each(function() {
    var id = this.id;
    $(this).autocomplete({
        minLength: 3,
        source: function(request, response) {
            alert(id);
        }
    });
});
alert( $(this.element).attr("id") );

作品。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM