我有这样的多个输入字段
<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
使用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);
}
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句