繁体   English   中英

在 jquery 中检查至少一个复选框验证

[英]check at least one checkbox validation in jquery

我有一些复选框,如下所示:

<form name="submit-form" name="submit-form">
<label>property1</label>
<input type="checkbox" name="property1"><br>
<label>property2</label>
<input type="checkbox" name="property2"><br>
<label>property3</label>
<input type="checkbox" name="property3"><br>
<label>property4</label>
<input type="checkbox" name="property4"><br>
<label>property5</label>
<input type="checkbox" name="property5"><br>
<label>property6</label>
<input type="checkbox" name="property6"><br>
<input type="submit">
</form>

我想一次验证至少一个复选框。 请帮助我。 提前致谢

$("#YourbuttonId").click(function(){
    if($('#YourTableId').find('input[type=checkbox]:checked').length == 0)
    {
        alert('Please select atleast one checkbox');
    }
});

更新:

我看到了你的代码,似乎你正在使用 jquery 验证插件,在这种情况下,这篇文章会对你有所帮助

并尝试为组中存在的复选框提供相同的名称

http://forum.jquery.com/topic/check-that-at-least-1-checkbox-is-checked-using-jquery-validate-plugin

我将它们全部包装在一个带有 div 的容器中,以便于选择,然后您可以使用:

if($('#wrapperID input:checked').length > 0) {
    'at least one is checked
}

尝试这个

$('form').submit(function(e) {
    if($("input:checked").length == 0){
    alert('please checked atleast one');
    }
});​

请参阅此演示: http : //jsfiddle.net/RGUTv/您的具体案例演示: http : //jsfiddle.net/J98Ua/

我在这里的旧回复: 验证至少一个复选框

希望休息能满足需要! :)

代码

$(document).ready(function() {
    $('#my-form').submit(function() {
        amIChecked = false;
        $('input[type="checkbox"]').each(function() {
            if (this.checked) {
                amIChecked = true;
            }
        });
        if (amIChecked) {
            alert('atleast one box is checked');
        }
        else {
            alert('please check one checkbox!');
        }
        return false;
    });
});​

暂无
暂无

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

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