zoukankan      html  css  js  c++  java
  • 单选按钮 设置required属性无法进行非空验证

    先看代码:

    <!DOCTYPE html>
    <html lang="en">
    <head>
    	<meta charset="UTF-8">
    	<title>demo</title>
    	<style>
    		input[type="submit"]{
    			 120px;
    			height: 40px;
    			border: 0;
    			background-color: antiquewhite;
    		}
    		input[type="submit"]:hover{
    			background-color: beige;
    		}
    	</style>
    </head>
    <body>
    	<form action="#" method="post">
    		<label><input type="radio" required />高中</label>
    		<label><input type="radio" required />大专</label>
    		<label><input type="radio" required />本科</label>
    		<label><input type="radio" required />硕士研究生</label>
    		<label><input type="radio" required />博士及以上</label>
    		<br />
    		<input type="submit" value="提交" />
    	</form>
    </body>
    </html>
    

     提交数据时并没有进行非空验证。

    正确代码为:

    <!DOCTYPE html>
    <html lang="en">
    <head>
    	<meta charset="UTF-8">
    	<title>demo</title>
    	<style>
    		input[type="submit"]{
    			 120px;
    			height: 40px;
    			border: 0;
    			background-color: antiquewhite;
    		}
    		input[type="submit"]:hover{
    			background-color: beige;
    		}
    	</style>
    </head>
    <body>
    	<form action="#" method="post">
    		<label><input type="radio" name="q1" required />高中</label>
    		<label><input type="radio" name="q1" required />大专</label>
    		<label><input type="radio" name="q1" required />本科</label>
    		<label><input type="radio" name="q1" required />硕士研究生</label>
    		<label><input type="radio" name="q1" required />博士及以上</label>
    		<br />
    		<input type="submit" value="提交" />
    	</form>
    </body>
    </html>
    

     

    原因:

    单项选择的实现是通过对多个单选按钮设置同样的name属性值和不同的选项值。例如,使用两个单选按钮,设置这两个控件的name值为sex,选项值一个为女,一个为男,从而实现从性别中选择一个的单选功能。

    单选按钮有一个重要的属性checked,用来设置或返回单选按钮的状态。一组name值相同的单选按钮中,如果其中一个按钮的checked属性被设置为true,则其他按钮的checked属性值就默认为false。

  • 相关阅读:
    HDU 1016 Prime Ring Problem
    CreateRemoteThread函数多參数传入用法
    Dynamics CRM2015 on-premises直接升级Dynamics CRM2016 on-premises
    cocos2d-x+lua代码热载入(Hot Swap)的研究
    DirectX 9.0c游戏开发手记之“龙书”第二版学习笔记之8: Chap10: Lighting
    js合并table单元格(拼table的时候并不知道详细几行几列)
    简单图模板 Graph
    POJ-3278-Catch That Cow-广搜(BFS)
    android用存到缓存的方法来保存ListView里的数据
    Ubuntu 14.10中连接Win10的smb共享文件
  • 原文地址:https://www.cnblogs.com/xiaohaodeboke/p/12259999.html
Copyright © 2011-2022 走看看