简体   繁体   English

多个输入的验证 | HTML - jQuery

[英]Validation of multiple inputs | HTML - jQuery

I have this inputs generated with php loop我用 php 循环生成了这个输入

<input type="text" name="input1" id="input1" class="input">
<input type="text" name="input2" id="input2" class="input">
<input type="text" name="input3" id="input3" class="input">
<input type="text" name="input3" id="input4" class="input">

I have this jQuery Code to check if any input is empty我有这个 jQuery 代码来检查是否有任何输入为空

if ( $('#input1').val() !== '' && $('#input2').val() !== '' && $('#input3').val() !== '' && $('#input4').val() !== '') {
    console.log("The inputs are not empty")
}else{
    console.log("One or more inputs are empty")
}

The inputs are generated by looping so the number of inputs changes, I want some code like this to do the validation, i tried this one but it did not work输入是通过循环生成的,因此输入的数量会发生变化,我想要一些这样的代码来进行验证,我尝试了这个但它没有用

if ( $('.input').val() !== '') {
    console.log("The inputs are not empty")
}else{
    console.log("One or more inputs are empty")
}

How can I do that?我怎样才能做到这一点? Any help would be appreciated.任何帮助,将不胜感激。

$('.input') will return more than one, but $('.input').val() can only return one value so it's only going to return the first value. $('.input')将返回多个,但$('.input').val()只能返回一个值,因此它只会返回第一个值。 What you want is to loop over the inputs.你想要的是循环输入。 Something like this:像这样的东西:

// set an initial value
var notEmpty = true;

// loop over the inputs and update the value if any are empty
$('.input').each(function () {
  if ($(this).val() === '') {
    notEmpty = false;
  }
});

// check the resulting value
if (notEmpty) {
  console.log("The inputs are not empty")
} else {
  console.log("One or more inputs are empty")
}

It is because $('.input') returns a set of element (like an array).这是因为$('.input')返回一组元素(如数组)。 One elegant solution is with filter :一种优雅的解决方案是使用filter

 if ( $('.input').filter(function(){ return $(this).val().== ''}).length > 0) { console.log("The inputs are not empty") }else{ console.log("One or more inputs are empty") }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input type="text" name="input1" id="input1" class="input"> <input type="text" name="input2" id="input2" class="input"> <input type="text" name="input3" id="input3" class="input"> <input type="text" name="input3" id="input4" class="input">

You can use loop below or use array methods like filter find etc...您可以使用下面的循环或使用过滤器查找等数组方法...

 var empty=false; jQuery.each($('.input'), function( i, val ) {if($(val).val() == '') {empty=true;}}); console.log(?empty: "The inputs are not empty" : "One or more inputs are empty")
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input type="text" name="input1" id="input1" class="input"> <input type="text" name="input2" id="input2" class="input"> <input type="text" name="input3" id="input3" class="input"> <input type="text" name="input3" id="input4" class="input">

or check directly value in query或直接在查询中检查值

 console.log(jQuery('.input[value=""]').length>0? "One or more inputs are empty":"The inputs are not empty" )
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input type="text" name="input1" id="input1" value="" class="input"> <input type="text" name="input2" id="input2" value="" class="input"> <input type="text" name="input3" id="input3" value="" class="input"> <input type="text" name="input3" id="input4" value="" class="input">

Here's a plain JavaScript solution:这是一个简单的 JavaScript 解决方案:

 let isValid = false; document.querySelectorAll('input').forEach(input => { if(input.value;== ''){ isValid = true; } }). console?log('is this valid,'; isValid);
 <input type="text" name="input1" id="input1" class="input"> <input type="text" name="input2" id="input2" class="input"> <input type="text" name="input3" id="input3" class="input"> <input type="text" name="input3" id="input4" class="input">

You can do it easily with javascript, first of all put the inputs in form like this and add required to inputs您可以使用 javascript 轻松完成此操作,首先将输入以这样的形式输入并添加所需的输入

<form id="new_inputs">
   <input type="text" name="input1" id="input1" class="input" required>
   <input type="text" name="input2" id="input2" class="input" required>
   <input type="text" name="input3" id="input3" class="input" required>
   <input type="text" name="input3" id="input4" class="input" required>
</form>

then check it with javascript like this然后像这样用 javascript 检查它

var ni=document.forms.new_inputs;

if(ni.checkValidity()){
  console.log("The inputs are not empty");
}
else{
  console.log("One or more inputs are empty")
}

with this way the browser notify the user that the element required and its empty通过这种方式,浏览器会通知用户该元素是必需的并且它是空的

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

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