检查是否至少使用HTML5选中了一个复选框

时间:2017-02-17 22:21:38

标签: html html5

我正在使用HTML5验证功能制作表单。

示例:

 url = dic["hits"][0]["recipe"]["url"]

我需要它来验证是否至少选中了一个复选框。

“必须至少选中一个复选框。”

我在这里找到答案:
Jquery - check if at least one checkbox is checked
Check if at least one checkbox is checked using jQuery
Making sure at least one checkbox is checked
Submit form only if at least one checkbox is checked

但是使用JavaScript,jQuery等等。

是否可以仅使用HTML5?

2 个答案:

答案 0 :(得分:0)

这是不可能的。您必须通过JS将函数绑定到onchange来检查它,该函数计算所选复选框的数量。复选框的required属性要求检查所有复选框(对于无线电复制,只需要检查一个值)。

答案 1 :(得分:0)

尝试以下代码.......

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Form Test</title>
</head>
<body>
    <form id="form1" method="post" action="">
        <label><input type="radio" name="color1" value="1" checked />Green</label><br />
        <label><input type="radio" name="color1" value="2" required />Red</label><br />
        <label><input type="radio" name="color1" value="3" required />Yellow</label><br />
        <label><input type="radio" name="color1" value="4" required />Blue</label><br />
        <input type="submit" /><input type="reset"/>
   </form>
</body>
</html>