简体   繁体   English

JavaScript-在当前模式下的任何输入/选择字段上检测输入更改

[英]JavaScript - detect input change on any input/select field on current modal

I have a modal with ~20 input and select fields that the user is supposed to complete. 我有一个〜20输入的模态,并选择用户应该完成的字段。 I would like to a quick JavaScript check whether the field is empty or not after the user is navigating away / changing / etc. the field, but want to avoid having to copy paste the code below 20 times and personalize it for each field. 我想快速JavaScript检查用户导航/更改/等字段后该字段是否为空,但想避免不得不复制粘贴以下20次代码并对每个字段进行个性化设置。

<!-- Holidex -->
<label>Holidex:</label>
<div class="input-group">
    <span class="input-group-addon"><i class="fa fa-bars"></i></span>
    <input type="text" class="form-control" maxlength="5" placeholder="What is your Holidex code?" id="addHolidex" name="addHolidex" style="text-transform:uppercase" />
</div>
<!-- /.Holidex -->

<script type="text/javascript">
$('#addHolidex').on('keyup keydown keypress change paste', function() {
  if ($(this).val() == '') {
    $('#addHolidex').removeClass('has-success').addClass('has-warning');
  } else {
    $('#addHolidex').addClass('has-success').removeClass('has-warning');
  }
});
</script>

Is there any way to have the code above check for any select / input field on my NewUserModal ? 有什么办法可以让上面的代码检查NewUserModal上的任何select / input字段?

Thank you! 谢谢!

EDIT 编辑

So I fiddled around with the suggested codes below but only the following managed to halfway work: 因此,我在下面摆弄了建议的代码,但只有以下代码成功完成了工作:

$('.input-group').on('keyup keydown keypress change paste', function() {
   if ($(this).val() == '') {
    $(this).removeClass('has-success').addClass('has-error');
  } else {
    $(this).addClass('has-success').removeClass('has-error');
  }
});

Empty fields are being flagged correctly now, but fields with content do not have the has-success class added. 现在正确标记了空字段,但是带有内容的字段没有添加has-success类。 Note that I have to apply this class to the <div class="input-group"> element instead of the input select fields. 请注意,我必须将此类应用于<div class="input-group">元素,而不是input select字段。

Any suggestions? 有什么建议么? I am running on bootstrap 3 if that helps. 如果有帮助,我正在使用Bootstrap 3。

EDIT 2 编辑2

Still no result and quite frankly have had enough for today. 仍然没有结果,坦率地说,今天已经受够了。 - select fields are either ignored or incorrectly flagged with has-error if pre-populated - individual input fields seem to work more or less - grouped input fields nestled in one div all turn red if one field is empty (eg. phone number + phone country both turn red of there is not country code entered) -如果预先填写, has-error忽略选择字段或将其错误标记为has-error error-各个输入字段似乎或多或少地起作用-如果一个字段为空,则嵌套在一个div分组输入字段全部变为红色(例如,电话号码+电话国家(地区)都变成红色(未输入国家代码)

// highlight empty fields in red
$('.input-group input, select').on('keyup keydown keypress change paste',function(){
   if ($(this).val() == '') {
    $(this).parent().closest('.input-group').removeClass('has-success').addClass('has-error');
  } else {
    $(this).parent().closest('.input-group').removeClass('has-error').addClass('has-success');
  }
});

I basically would have to redo the whole design of my modal and I quite frankly dont want to go down that road. 我基本上必须重做我的模型的整个设计,并且坦率地说,我不想走那条路。 Not a fan of JS/ Jquery today. 今天不喜欢JS / Jquery。

Not really sure this is what you're looking for but, why do not simply make your code more universal: 并不是很确定这是您要寻找的,但是,为什么不简单地使您的代码更通用:

$('input').on('keyup keydown keypress change paste', function() {
    if ($(this).val() == '') {
        $(this).removeClass('has-success').addClass('has-warning');
    } else {
        $(this).addClass('has-success').removeClass('has-warning');
    }
});

EDIT 编辑

If you would like to specify a precise form, add an ID to your form : 如果您想指定一个精确的表格,请在表格中添加一个ID:

<form id="myForm">
    <label>Holidex:</label>
    <div class="input-group">
        <span class="input-group-addon"><i class="fa fa-bars"></i></span>
        <input type="text" class="form-control" maxlength="5" placeholder="What is your Holidex code?" id="addHolidex" name="addHolidex" style="text-transform:uppercase" />
    </div>
</form>

$('#myForm input').on('keyup keydown keypress change paste', function() {
    if ($(this).val() == '') {
        $(this).removeClass('has-success').addClass('has-warning');
    } else {
        $(this).addClass('has-success').removeClass('has-warning');
    }
});

Add a new class to the input 在输入中添加一个新类

<input type="text" class="form-control Input-to-verify" maxlength="5" placeholder="What is your Holidex code?" id="addHolidex" name="addHolidex" style="text-transform:uppercase" />

and then in javascript: 然后在javascript中:

   $('.Input-to-verify').on('change',function(){
   if ($(this).val() == '') {
    $(this).removeClass('has-success').addClass('has-warning');
  } else {
    $(this).addClass('has-success').removeClass('has-warning');
  }
});

I hope this works 我希望这行得通

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM