仅在选中至少一个复选框时提交表单 [英] Submit form only if at least one checkbox is checked
本文介绍了仅在选中至少一个复选框时提交表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是triyng验证表单。
在这种形式中,你必须通过复选框选择至少一个元素,我不能确定它们的数量(它取决于元素数量)。
如果选中一个或多个复选框,我需要启用提交输入,如果没有选中任何复选框,则禁用它,我该怎么办?
这是我的代码:
i'm triyng to validate a form. In this form you've to choose at least one element by checkboxes, and I can't be sure about their quantity (it depends by elements number). I need to enable the submit input if one or more checkboxes are checked, and disable it if there aren't any checkbox checked, how can I do? Here's my code:
<form id="booking">
<input type="checkbox" name="room1" class="roomselect"/>
<input type="checkbox" name="room2" class="roomselect"/>
<input type="checkbox" name="room3" class="roomselect"/>
<input type="submit" value="Request" id="subm" />
</form>
推荐答案
//dom ready handler
jQuery(function ($) {
//form submit handler
$('#booking').submit(function (e) {
//check atleat 1 checkbox is checked
if (!$('.roomselect').is(':checked')) {
//prevent the default form submit if it is not checked
e.preventDefault();
}
})
})
这篇关于仅在选中至少一个复选框时提交表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文