简体   繁体   English

jQuery UI自动完成-访问id属性时出现问题

[英]jQuery UI autocomplete - problems accessing id attribute

I'm using this: http://docs.jquery.com/UI/Autocomplete 我正在使用这个: 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. source回调中,我需要知道当前元素的ID。 In this example, it's obviously foo ; 在这个例子中,显然是foo ; in the real application, the selector is dynamically assigned so I don't know what the id is. 在实际的应用程序中,选择器是动态分配的,所以我不知道id是什么。

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? 如何获取元素的ID?

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: 您可以使用以下方法,而不是为要autocomplete所有输入分配变量名称:

$('#some_id').autocomplete {
  source: function() {
    var element = this.element[0];
    var element_id = this.element[0].id;
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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