使用jQuery验证验证多个复选框

wow*_*uzz 5 jquery jquery-validate

我试图采取一些复选框,并确保使用jQuery验证至少检查其中一个复选框.到目前为止我没有运气.我错过了什么?我知道我的验证是因为它适用于其他领域,而不是我的复选框.我把代码放在jfiddle上,也许这会有所帮助.

编辑:我为我的输入名称=列表参数(列表[])添加了括号.同样在我的规则中,我将名称参数从列表更改为'list []'.我的旧代码如下.谢谢Sparky!

OLD: <input type='checkbox' name='list' id='fullProduct'></input>

FIXED: <input type='checkbox' name='list[]' id='fullProduct'></input>
Run Code Online (Sandbox Code Playgroud)

这是我的代码.

$("#tradeForm").validate({
        rules: {
            startDate: {
                required: true,
                date: true
            },
            endDate: {
                required: true,
                date: true
            },
            showName: {
                required: true,
                minlength: 5
            },
            location: {
                required: true
            },
            list: {
                required: true
            }
        },
        messages: {
            startDate: "*",
            endDate: "*"
    }
});

                <table>
                <tr>
                    <th>Name of Show</th>
                    <td> <input type='text' name='showName'></input></td>
                </tr>
                <tr>
                    <th>Location</th>
                    <td><input type='text' name='location'></input></td>
                </tr>
                <tr>
                    <th><span style='padding-right: 50px;'>Select Literature</span></th>
                    <td><input type='checkbox' name='list' id='fullProduct'></input><span style='font-size: 12px;'>Guide One</span></td>
                    <td><input type='checkbox' name='list' id='oilProduct'></input><span style='font-size: 12px;'>Guide Two</span></td>
                </tr>                               
                <tr>                                
                    <td></td>                       
                    <td><input type='checkbox' name='list' id='diamondProduct'></input><span style='font-size: 12px;'>Guide Three</span></td>
                    <td><input type='checkbox' name='list' id='motorProduct'></input><span style='font-size: 12px;'>Guide Four</span></td>
                </tr>                               
            </table>
Run Code Online (Sandbox Code Playgroud)

Spa*_*rky 16

name您的复选框组的是list[],不是list,所以你必须声明的规则本身.由于它包含括号,因此[]必须将其括在引号中:

rules: {
    'list[]': {
        required: true
    }
},
Run Code Online (Sandbox Code Playgroud)

你的jsFiddle:http: //jsfiddle.net/ZDz59/1/