表单上所有复选框的jQuery验证代码

时间:2011-09-29 16:49:42

标签: jquery message validation checkbox

我有一个表格,可以生成一个记录列表,旁边有每个记录的复选框。

如果用户没有检查任何记录并点击提交,那么对话框应该警告他说“你没有检查任何记录”。如果他选择继续,那么他将被重定向到下一页,否则他将被保持在同一页面上以选择其余的记录。

我对jQuery和javascript不太满意。任何帮助将不胜感激。

我正在寻找的逻辑是这样的

提交按钮

<script>
if(
 (input:checkbox).count = (input:checkbox).is(checked).count
 //proceed to the next page
else(
 dialogue("Message")
)
</script>

2 个答案:

答案 0 :(得分:2)

首先,让我们假设你的表单有id =“myForm”:

$('#myForm').submit(function()
{
    if($('#myForm input:checkbox:checked').length == 0)
    {
        //Tell the user he/she needs to check some boxes
        return false; // this stops the form from being submitted
    } 
});

编辑:记得把所有这些都放在

$('document').ready(function(){

});

答案 1 :(得分:0)

在jQuery中

这将是:
    

    
$(function(){
$('#submit').click(function(){
if($('input:checkbox:checked').length > 0){
// next
}else{
alert('check a box');
}
});
});