简体   繁体   English

检查是否至少填充了一行

[英]Check if at least one row is filled

I have 3 rows with 3 inputs in each. 我有3行,每行3个输入。 What I need is to check if at least in one row all inputs are filled. 我需要检查是否至少一行中的所有输入都已填充。 If one row is filled then we go to other page. 如果一行被填满,那么我们转到另一页。 There is no difference between which row is filled, it could be even last row. 哪一行被填充之间没有区别,甚至可以是最后一行。 Here's my piece of code: 这是我的代码:

             <div class="div-rows row-1">
                <input name="kfz_first_name" type="text" placeholder="Vorname" data-input="1"/>
                <input name="kfz_last_name" type="text" placeholder="Nachname" data-input="1"/>
                <input name="kfz_birthday" class="datepicker" type="text" placeholder="Geburtsdatum" data-input="1"/>
                <div class="clear"></div>
            </div>
            <div class="div-rows row-2">
                <input name="kfz_first_name" type="text" placeholder="Vorname" data-input="2"/>
                <input name="kfz_last_name" type="text" placeholder="Nachname" data-input="2"/>
                <input name="kfz_birthday" class="datepicker" type="text" placeholder="Geburtsdatum" data-input="2"/>
                <div class="clear"></div>
            </div>
            <div class="div-rows row-3">
                <input name="kfz_first_name" type="text" placeholder="Vorname" data-input="3"/>
                <input name="kfz_last_name" type="text" placeholder="Nachname" data-input="3"/>
                <input name="kfz_birthday" class="datepicker" type="text" placeholder="Geburtsdatum" data-input="3"/>
                <div class="clear"></div>
            </div>
            <br/>
            <a href="#" class="x-btn">click</a>

        var zaza = true;
        $('.div-rows input').each(function(){
            if ($.trim($(this).val()) == "") {
                $(this).addClass('red-border');
                zaza = false;
            }
            if (zaza == false) {
                return false;
            }
        });

You have a scope issue. 您遇到范围问题。 return false breaks the $.each part. return false会破坏$.each部分。 Check for false after iterating. 迭代后检查是否为假。

var zaza = true;
$('.div-rows input').each(function(){
    if ($.trim($(this).val()) == "") {
        $(this).addClass('red-border');
        zaza = false;
    }
});
if (zaza == false) {
    return false;
}

Something like this should work, not tested, but you'll get the idea. 像这样的事情应该起作用,未经测试,但是您会明白的。

$(document).ready(function() { 
  $(document).on('click', '.x-btn', function() { 
    var canContinue= true;

    $('.div-rows').each(function() { 
      var allInRowsAreEmpty = true;

      $('input', $(this)).each(function() { 
        if($(this).val() != '') { // $(this) = the current input in the current .div-rows
          allInRowAreEmpty = false;
        }
      });

      if(allEmpty) {
        $(this).addClass('your-error-class'); // $(this) is the current .div-rows
        canContinue= false;
      }
    });

    if(canContinue) {
      // do your thing
    }
  }
});

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

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