2016-10-29 5 views
0

私はjQueryオートコンプリートを使用しています。 私のページには10個のテキストボックスがあります。私はクラス名を使ってオートコンプリートをバインドしました。 ソースメソッドでは、ユーザが入力した値に基づいてデータを取得するサービスを呼び出しています。サービスに2つのパラメータを渡す必要があります。 1.ユーザーが入力した値。 2.テキストボックスの親に格納されているデータ。jqueryオートコンプリートで現在の要素を取得するには?

どうすれば入手できますか?

<div data-id="101"> 
    <input class="search_txt" type="text"/> 
</div> 
<div data-id="102"> 
    <input class="search_txt" type="text"/> 
</div> 
..... 
... 
... 
.... 
$('.search_txt').autocomplete({ 
     minLength: 1, 
     source: function(request, response) {                    
     //service here 
     //need to get "data-id" of current using text box. 
     }, 
     focus: updateTextBox 
     select: updateTextBox 
}); 

ありがとうございます。

答えて

0

私は右に理解された場合は、この使用します。それは、クラス名.search_txtに焦点を当てている入力が、それは親の選択を取得し、データ-ID

例の属性を取得します

$('.search_txt:focus').parent().attr('data-id') 

を:

$('.search_txt').autocomplete({ 
 
     minLength: 1, 
 
     source: function(request, response) {            
 
     console.log("Data-id: "+$('.search_txt:focus').parent().attr('data-id')); 
 
     //service here 
 
     //need to get "data-id" of current using text box. 
 
     } 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> 
 

 
<div data-id="101"> 
 
    <input class="search_txt" type="text"/> 
 
</div> 
 
<div data-id="102"> 
 
    <input class="search_txt" type="text"/> 
 
</div>

関連する問題