我正在尝试使用多个复选框,并确保使用jQuery验证至少选中其中一个复选框。到目前为止,我还没有任何运气。我遗漏了什么?我知道我的验证在那里,因为它适用于其他字段,只是不适用于我的复选框。我把代码放到jfiddle上了,也许这会有帮助。
编辑:我为我的输入name=list参数(list[])添加了括号。另外,在我的规则中,我将name参数从list更改为'list[]‘。我的旧代码如下所示。谢谢你,Sparky!
OLD: <input type='checkbox' name='list' id='fullProduct'></input>
FIXED: <input type='checkbox' name='list[]' id='fullProduct'></input>这是我的代码。
$("#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>发布于 2013-02-20 04:14:43
如果您只想始终选中其中一个复选框,请改用input type="radio"。
如果没有,请尝试将复选框的名称属性更改为list[]。因为可以有多个校验值,所以它必须包含括号以指示它是一个数组。否则,该值将被覆盖。
https://stackoverflow.com/questions/14966465
复制相似问题