检查是否至少使用HTML5选中了一个复选框 [英] Check if at least one checkbox is checked using HTML5 only

查看:78
本文介绍了检查是否至少使用HTML5选中了一个复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



示例:

 <!DOCTYPE html> 
< html>
< head>
< meta charset =utf-8>
< title>表单测试< / title>
< / head>
< body>
< form id =form1method =postaction =>
< label>< input type =checkboxname =color1value =1required /> Green< / label>< br />
< label>< input type =checkboxname =color2value =2required /> Red< / label>< br />
< label>< input type =checkboxname =color3value =3required />黄色< / label>< br />
< label>< input type =checkboxname =color4value =4required /> Blue< / label>< br />
< input type =submit/>< input type =reset/>
< / form>
< / body>
< / html>

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



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



我在这里找到答案:

Jquery - 检查是否至少选中了一个复选框

检查是否使用jQuery检查至少一个复选框

确保选中至少一个复选框

仅在选中至少一个复选框时才提交表单



但是使用JavaScript,jQuery等



可以使用HTML5 onl是吗?

解决方案

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



< pre class =snippet-code-html lang-html prettyprint-override> <!DOCTYPE html>< html>< head> < meta charset =utf-8> < title> Form Test< / title>< / head>< body> < form id =form1method =postaction => < label>< input type =radioname =color1value =1checked /> Green< / label>< br /> < label>< input type =radioname =color1value =2required /> Red< / label>< br /> < label>< input type =radioname =color1value =3required />黄色< / label>< br /> < label>< input type =radioname =color1value =4required /> Blue< / label>< br /> < input type =submit/>< input type =reset/> < / form>< / body>< / html>


I'm making a form using HTML5 validation features.

Example:

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

I need it to validate if at least one checkbox is checked.

"Must have at least one checkbox checked."

I found answer here:
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

But using JavaScript, jQuery and etc.

Is possible make using HTML5 only?

解决方案

Try The following Code.......

<!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>

这篇关于检查是否至少使用HTML5选中了一个复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆