简体   繁体   中英

jQuery UI autocomplete - problems accessing id attribute

I'm using this: http://docs.jquery.com/UI/Autocomplete

I'm initializing a text box like this (partial code):

$('#foo').autocomplete
{
     source: function()
     {
          // How to get the id of the element?
     }
}

In the source callback, I need to know what the id is of the current element. In this example, it's obviously foo ; in the real application, the selector is dynamically assigned so I don't know what the id is.

I looked at the following:

console.debug($(this)); // displays [a.widget.a]
console.debug($(this.id)); // displays []
console.debug($(this).attr('id')); // displays undefined

How to get the id of the element?

You could just use...

var element = $('#one');

element.autocomplete
{
     source: function()
     {
          var id = element.id;
     }
}

Rather than assigning variables names to all the inputs you want autocomplete on, you can use:

$('#some_id').autocomplete {
  source: function() {
    var element = this.element[0];
    var element_id = this.element[0].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