8

私はこれにしばらく取り組んできましたが、どこにも行けないようです。基本的に、クラスごとに一連の入力のオートコンプリートがありますが、ajaxを投稿するオブジェクトをビルドするために特定の入力IDを取得する必要があります(このプロジェクトではGETではなくPOSTを使用する必要があります)。

$(".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
});
4

4 に答える 4

14

試す:

$(this.element).prop("id");

また:

this.element[0].id;

sourceコールバック内でthis、ウィジェット インスタンスを参照します。ウィジェットが接続されている要素を取得するにthis.elementは、jQuery オブジェクトである を使用する必要があります。

于 2012-10-13T01:47:47.680 に答える
4

私は自分のプロジェクトでそれを試しました。これは私のために働いた:

$(this.element.get(0)).attr('id');
于 2012-10-13T02:15:00.683 に答える
3

私はあなたに似た状況にuser1572796ありましたが、これが私にとってうまくいきました:

$(this).prop("id");
于 2016-06-08T20:20:30.230 に答える