简体   繁体   English

HTML5必填验证复选框

[英]Html5 required validation checkbox

I made a input checkbox that contains array values. 我做了一个包含数组值的输入复选框。 so it generates plenty of rows in a table. 因此它会在表中生成大量行。 But it needs for me to check it all to submit. 但是我需要检查所有内容以提交。 It doesn't allow me to check only few not all. 它不允许我仅检查一些而不是全部。

<form>
<table>

<td>
<input required="required" type="checkbox" name="id[]" id="id" value="<?php  echo $result2["id"]; ?>"/>
</td>

<input type="submit" name="submit" value="submit"/>

</table>
</form>

How can i make the required field able to check atleast one and able to submit even if not all are checked? 我如何才能使必填字段能够至少检查一个并提交,即使没有全部选中呢?

I dont know if I understand you, 我不知道我是否了解你,
but maybee you need something like this: 但是也许您需要这样的东西:
Online demo 在线演示

Main part with notes: 带有注释的主要部分:

$('#frm').bind('submit', function(e) { // set this function for submit for your formular
    validForm = true; // default value is that form is correct, you can chcange it as you wish
    var n = $( "input:checked" ).length; // count of checked inputs detected by jQuery
    if (n != 1) { validForm=false; } // only if you checked 1 checkbox, form is evaluated as valid
    if (!validForm) { // if result of validation is: invalid
        e.preventDefault(); // stop processing form and disable submitting
    }
    else {
        $('#echo').html("OK, submited"); // ok, submit form
    }
});

You can use any number of comboboxes under any tag and get count of selected by jQuery, then use any rule for validate form. 您可以在任何标签下使用任意数量的组合框,并获得jQuery选择的计数,然后使用任何规则来验证表单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM