开发者

How to make sure at least 1 checkbox is checked with jQuery Validate

I have a group of checkboxes that each have their own name, so they are not a checkbox group. I need a custom method for jQuery Validate that will make sure 1 or more is checked. Also, typically you tie a validation method to开发者_运维知识库 a field, but this covers lots of fields, so is therea jQuery Validate function that allows me to tie this to the submit so that it runs along with the other typical field required checks when the submit button is pressed?

Example checkboxes:

<form id="form" name="form>
<input type="checkbox" name="app1" value="y" />App 1
<input type="checkbox" name="app2" value="y" />App 2
<input type="checkbox" name="app3" value="y" />App 3
<input type="checkbox" name="app4" value="y" />App 4
<input type="submit" value="Submit">
</form>


You can just check to see if any of the forms children are checked.

if (!$('#form').children(':checked').length) return false;

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜