关于类的jQuery自动完成,如何获取id

use*_*796 8 jquery jquery-ui jquery-autocomplete jquery-ui-autocomplete

我一直在研究这个问题,似乎无处可去.基本上我在类的一堆输入上有自动完成,但是我需要获取特定的输入id来构建对象以发布ajax(我必须为这个项目使用POST而不是GET).

$(".input_autocomplete").autocomplete({
  source: function( request, response ) {
  // here is where I get the hash from local storage,
  // reference the id and build the object
  // tried this.id, $(this).prop('id'), no luck
  $.ajax({
    url: '/somepath/filename.htm',
    contentType: 'application/json',
    dataType: 'json',
    type: 'POST',
    data: JSON.stringify(obj),
    success: function(json) {
      return {
        label: item.label,
        value: item.label
      }
    },
    error: function() {
      // error handling
    }
   }); // ajax
 } // source
});
Run Code Online (Sandbox Code Playgroud)

And*_*ker 12

尝试:

$(this.element).prop("id");
Run Code Online (Sandbox Code Playgroud)

要么:

this.element[0].id;
Run Code Online (Sandbox Code Playgroud)

source回调内部,this指的是小部件实例.为了获得小部件附加到的元素,您应该使用this.element,这是一个jQuery对象.

  • @ 7th:请看我的更新.自动完成内部的`this`是小部件实例,因此`this.element`是它应用的元素.`element.get(0)`将获取jQuery对象的第一个元素,相当于上面的`element [0]`. (2认同)
  • $(this.element).prop( "ID"); 工作得很好,谢谢! (2认同)