简体   繁体   English

检查以确保所有字段都已填写

[英]Check to make sure all fields are filled Jquery

I'm trying to make an easy validator in jquery for my input fields. 我正在尝试在jquery中为我的输入字段创建一个简单的验证器。 Currently i got the following: 目前我得到以下内容:

function checkInputs(){
var isValid = true;
$('.input-required').each(function() {
     if($(this).val() === ''){
        isValid = false;
         return false;
     }        
 });
return isValid;
}

And then i got a button right that is this: 然后我得到了一个正确的按钮,它是:

$('#confirm').click(function () { 
    alert(checkInputs());
});

But this always returns true even if the input is empty. 但是,即使输入为空,也始终返回true。 Also after this works am going to make to where if all inputs are filled in, a button will be enabled to click on. 同样,在完成所有输入后,将启用一个按钮来单击。

edited it so it has a selector now, still getting always true. 编辑了它,使其现在具有选择器,但仍然始终为true。

Thanks in advance 提前致谢

Try use the filter attribute to get the inputs that has a required attribute. 尝试使用filter属性来获取具有required属性的输入。

$('input').filter('[required]')

Added code to check if inputs are filled and enable or disable button . 添加了代码以检查输入是否已填充以及启用或禁用button Note if we use this, there aint much point of the $('#confirm').click(function()); 请注意,如果使用此选项,则$('#confirm').click(function());不大$('#confirm').click(function()); function since this button will only be enabled when the inputs are filled. 功能,因为仅当输入被填充时,此按钮才会启用。

 function checkInputs() { var isValid = true; $('input').filter('[required]').each(function() { if ($(this).val() === '') { $('#confirm').prop('disabled', true) isValid = false; return false; } }); if(isValid) {$('#confirm').prop('disabled', false)} return isValid; } $('#confirm').click(function() { alert(checkInputs()); }); //Enable or disable button based on if inputs are filled or not $('input').filter('[required]').on('keyup',function() { checkInputs() }) checkInputs() 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form> <input required> <input required> <input required> <button id="confirm">check</button> </form> 

Try to target the element in this way: $('input[required]') 尝试通过以下方式定位元素: $('input[required]')

This should do the trick. 这应该可以解决问题。

Your selector is looking for a tagName <input-required></input-required> that obviously doesn't exist. 您的选择器正在寻找显然不存在的tagName <input-required></input-required>

Add a dot prefix for class 为课程添加点前缀

Can also use filter() to simplify 也可以使用filter()来简化

function checkInputs(){
    return !$('.input-required').filter(function() {
         return !this.value;   
     }).length;
}

NOTE: Will not work on radios or checkbox if those are part of the collection of elements with that class and you would need to add conditional for type if that is the case 注意:如果单选框或复选框是该类元素集合的一部分,则将无法使用,并且在这种情况下,您需要为类型添加条件

if it is 0 then all input filled otherwise it will return 0 mean any one or more are empty input. 如果为0,则所有输入已填充,否则将返回0,表示任何一个或多个为空输入。

 function checkInputs(){
  var flag = 0;
  $('input').each(function() {
       if($(this).val() == ''){
         return flag = 1;
       }
   });
  return flag;
  }
  $('#confirm').click(function () {
      alert(checkInputs());
  });

You forgot to put class symbol in jQuery: 您忘记将类符号放在jQuery中:

function checkInputs() {
    var isValid = true;
    $('.input-required').each(function() {
     if($(this).val() === ''){
        isValid = false;
         return false;
     }        
  });

  return isValid;
}

Try this.. 尝试这个..

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

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