简体   繁体   English

如何使用jQuery检查选择框或文本输入是否为空

[英]How to check if either select box or text inputs are empty with jQuery

I have a form with 2 select boxes and a text input. 我有一个带有2个选择框和一个文本输入的表单。 On change of any of those 3 elements I want to run a check to see if any of the elements are empty(no value) and then change a variable. 在更改这3个元素中的任何一个时,我想运行一次检查以查看是否有任何元素为空(无值),然后更改一个变量。 Here's what I have tried so far: 到目前为止,这是我尝试过的:

 var emptyFields = true; var inputs = $('input[type="text"], select').on('keyup change', function() { inputs.each(function() { var elm = $(this), val = elm.val(); if ((val != '0' && elm.is('select')) || (val != '' && elm.is('input'))) { emptyFields = false; return false; } }); $('.info span').text(emptyFields); }); 
 .info { margin-top:20px; } span { font-weight:bold; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select> <option selected disabled>SELECT AN OPTION</option> <option>One</option> <option>Two</option> <option>Three</option> </select> <select> <option selected disabled>SELECT AN OPTION</option> <option>Four</option> <option>Five</option> <option>Six</option> </select> <input type="text" /> <div class="info">Empty fields? <span></span></div> 

The issue I am having now is that the variable is changing to false even before all 3 elements have a value. 我现在遇到的问题是,即使在所有3个元素都具有值之前,变量也将变为false If you are completing the elements from left to right, the variable should not return false until you've entered a character into the text input after making a selection in each select box. 如果要从左到右完成元素,则在每个select框中进行选择后,在text input字符之前,变量不应返回false

First problem: Once you set the variable to false during a check, you never change it back to true in future checks. 第一个问题:一旦在检查期间将变量设置为false ,就永远不会在以后的检查中将其更改为true You should use a local variable, which you initialize each time you call the function. 您应该使用局部变量,每次调用该函数时都要对其进行初始化。

Second problem: You have your check backwards. 第二个问题:您的支票向后退。 Since you want to show true if any of the fields are empty, you need to initialize the variable to false , then set it to true when you find an empty field. 由于要在任何字段为空的情况下显示true ,因此需要将变量初始化为false ,然后在找到空字段时将其设置为true

 var inputs = $('input[type="text"], select').on('keyup change', function() { var emptyFields = false; inputs.each(function() { var elm = $(this), val = elm.val(); if ((val == '0' && elm.is('select')) || (val == '' && elm.is('input'))) { emptyFields = true; return false; } }); $('.info span').text(emptyFields); }); 
 .info { margin-top:20px; } span { font-weight:bold; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select> <option selected disabled>SELECT AN OPTION</option> <option>One</option> <option>Two</option> <option>Three</option> </select> <select> <option selected disabled>SELECT AN OPTION</option> <option>Four</option> <option>Five</option> <option>Six</option> </select> <input type="text" /> <div class="info">Empty fields? <span></span></div> 

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

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