2015-06-17 70 views
1

我有多個輸入字段這樣選擇屬性值和字段值使用jQuery自動完成

<input type="text" name="loksabha" data-name="loksabha" class="form-control lokVidhan" placeholder="Loksabha"><br><br> 

<input type="text" name="zila" data-name="zila" class="form-control lokVidhan" placeholder="Zila"> 

<script> 
    $('.lokVidhan').autocomplete({ 
     source: function(request, response){ 
      var name = $.trim(request.term); 
      var data_name = $('.lokVidhan').attr('data-name'); 
      console.log(data_name); 
     } 
    }); 
</script> 

我想用選擇當前字段值:

var name = $.trim(request.term); 

這是工作的罰款,但我也想要選擇:

var data_name = $('.lokVidhan').attr('data-name');` 

但它只給我第一個字段值。我想要當前的字段值。我也試試這個:

var data_name = $(this).val(); // result: undefined 
+0

Thak你羅裏McCrossan – daulat

回答

3

使用each(),這樣就可以保持上下文中的變量中,每次重複像

$('.lokVidhan').each(function() { 
      var that = $(this); 
      that.autocomplete({ 
       source: function(request, response) { 
        var name = $.trim(request.term); 
        var data_name = that.attr('data-name'); 
        console.log(data_name); 
       } 
      }); 
相關問題