簡體   English   中英

jQuery UI自動完成-訪問id屬性時出現問題

[英]jQuery UI autocomplete - problems accessing id attribute

我正在使用這個: http : //docs.jquery.com/UI/Autocomplete

我正在初始化一個這樣的文本框(部分代碼):

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

source回調中,我需要知道當前元素的ID。 在這個例子中,顯然是foo ; 在實際的應用程序中,選擇器是動態分配的,所以我不知道id是什么。

我看了以下內容:

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

如何獲取元素的ID?

您可以使用...

var element = $('#one');

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

您可以使用以下方法,而不是為要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