简体   繁体   English

检查每个输入是否有价值

[英]check every input has value

Html HTML

<input type="text" name="test[]" class="test">

How can I check every input has value? 如何检查每个输入是否有价值? This is what I tried but if one the input has value it will not alert the message 这是我尝试过的方法,但是如果其中一个输入有值,它将不会提示该消息

Script 脚本

var x = new Array();
var message = "";

jQuery("input[name='test[]']").each(function(){
  var y = jQuery(this).val().toLowerCase();
  if(y){
    x.push(y);
  }
});

if(x.length === 0){
  message += "missing";
};

if(message != ""){
  alert(message);
}

EDIT I want to alert if one of input has no value 编辑我想提醒输入之一没有值

This will alert 'no value' if one of the inputs has no value. 如果输入之一无值,这将警告“无值”。 Just check the $.val() and alert and break the loop if it doesn't return anything. 只需检查$.val()$.val()警报并中断循环(如果它不返回任何内容)。

 $('button').on('click', function() { $("input[name='test[]']").each(function() { if (!$(this).val()) { alert('no value'); return false; } }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="text" name="test[]" class="test"> <input type="text" name="test[]" class="test"> <input type="text" name="test[]" class="test"> <button>validate</button> 

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

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