【问题标题】:check at least one checkbox validation in jquery在 jquery 中检查至少一个复选框验证
【发布时间】:2012-10-17 08:35:18
【问题描述】:

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

<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>

我想一次验证至少一个复选框。请帮助我。在此先感谢

【问题讨论】:

  • 请放置实际的html

标签: jquery forms checkbox


【解决方案1】:
$("#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

【讨论】:

  • 它只在我刷新页面时有效,在提交表单时无效。
  • 它不起作用。还有一些其他的验证,除了这个,所有的都在工作
  • royalgroupinternational.com/add_enquiry查看这个html的源代码。
  • 你想要这个复选框组
  • 谢谢,你帮了我很多...该链接帮助我解决了问题,所有功劳归于你。再次感谢...
【解决方案2】:

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

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

【讨论】:

  • 好的。然后把身份证放在桌子上。
【解决方案3】:

试试这个

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

【讨论】:

    【解决方案4】:

    查看此演示:http://jsfiddle.net/RGUTv/您的具体案例演示:http://jsfiddle.net/J98Ua/

    我的旧回复:validation for at least one checkbox

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

    代码

    $(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;
        });
    });​
    

    【讨论】:

    • 它不起作用。我知道你在此之前解决了许多复选框验证问题,但它不起作用,我不希望它处于警报状态......
    • @Ravneet'Abid' 很酷的人,大声笑你能做一个你的弹跳JQ代码问题的演示吗?让我知道哪个部分实际上不起作用。
    猜你喜欢
    • 2012-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-27
    • 2021-07-23
    • 1970-01-01
    相关资源
    最近更新 更多