我有多个像这样的输入字段
<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 :(得分: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);
}
});