简体   繁体   English

验证在动态添加的字段上选择的至少一个复选框

[英]validating at least one checkbox selected on dynamically added fields

If you're dynamically adding form fields to an existing form, what's the best way of adding validation? 如果您要将表单字段动态添加到现有表单,添加验证的最佳方法是什么?

Consider this fiddle: http://jsfiddle.net/yWGK4/ 考虑一下这个小提琴: http//jsfiddle.net/yWGK4/

<form action="#" method="post">
<div id="parent">
    <div class="child">
        <input type="checkbox" name="box1[]" value="1" /> 1
        <input type="checkbox" name="box1[]" value="2" /> 2
        <input type="checkbox" name="box1[]" value="3" /> 3
    </div>
</div>
</form> 
<button id="addBoxes">Add Boxes</button>

<script>
$(function() {
    var parentdiv = $('#parent');
    var m = $('#parent div.child').size() + 1;
    $('#addBoxes').on('click', function() {
        $('<div class="child"><input type="checkbox" name="box'+m+'[]" value="1" /> 1 <input type="checkbox" name="box'+m+'[]" value="2" /> 2 <input type="checkbox" name="box'+m+'[]" value="3" /> 3 </div>').appendTo(parentdiv);
        m++;
        return false;
    });
});
</script>

On that form I'm adding new checkbox groups, and want to make sure at least one box from each group is checked (not one box across all groups). 在该表单上,我添加了新的复选框组,并希望确保每个组中至少有一个框被选中(不是所有组中的一个框)。 Anyone got any clever methods? 有人有任何聪明的方法吗? Everything I've looked at would get very complicated due to the dynamically added fields. 由于动态添加字段,我所看到的一切都会变得非常复杂。

It doesn't matter if the checkboxes are dynamic when validating on submit etc. so something like this would check if at least one checkbox per .child is checked : 在提交等时验证复选框是否是动态的并不重要。所以这样的事情会检查每个.child是否至少检查一个复选框:

$('form').on('submit', function(e) {
    e.preventDefault();
    var valid = true;

    $('.child').each(function() {
        if ( ! $('[type="checkbox"]:checked', this).length ) // no box checked
            valid = false;
    });

    if (valid) {
        this.submit();
    }else{
        alert('error');
    }
});

FIDDLE 小提琴

If you want to check it using jQuery you can use `.each() 如果你想使用jQuery检查它,你可以使用`.each()

$(".child").each(function(){
   var $this = $(this);
   var checked = $this.find("input:checked");
   if( checked.lenght == 0 ) {
      alert("This group is not valid");
   }
});

You can find more about this jQuery functions in this links: each() 您可以在以下链接中找到有关此jQuery函数的更多信息: each()

find() 找()

here's what I came up with. 这就是我想出来的。 Basically you loop over the .child groups and test if they have a checkbox in the checked state.. 基本上你循环遍历.chi​​ld组并测试它们是否在选中的状态下有一个复选框。

 $('#checkBoxes').on('click', function(){
    var uncheckedgroups = new Array();
    $('.child').each(function(childindex, childelement){
        var checkFound = 0;
        $('.child').each(function(index, element){
            if($(element).is(':checked')){
                checkFound = 1;
            }
        });
        if(checkFound == 0){
            uncheckedgroups.push(childindex);                
        }
    });
    if(uncheckedgroups.length > 0){
    alert("the following groups have no checked checkbox: " +
         uncheckedgroups.join(','));
    }
});

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

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