标签作用(四)
20181117
一、单选框和复选框
在使用表单设计调查表时,为了减少用户的操作,使用选择框是一个好主意,html中有两种选择框,即单选框和复选框,两者的区别是单选框中的选项用户只能选择一项,而复选框中用户可以任意选择多项,甚至全选。
<input type=”radio/checkbox” value=”值” name=”名称” checked=”checked” />
1、type:
当 type="radio" 时,控件为单选框
当 type="checkbox" 时,控件为复选框
2、value:提交数据到服务器的值(后台程序PHP使用)
3、name:为控件命名,以备后台程序 ASP、PHP 使用
4、checked:当设置 checked="checked" 时,该选项被默认选中。一般用在单选框上,提前给用户一个默认值,提高用户体验。
5、同一个组的单选按钮,name取值要一致。
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>单选框、复选框</title>
</head>
<body>
<form action="save.php" method="post" >
<label>性别:</label>
<label>男</label>
<input type="radio" value="1" name="gender" checked="checked" />
<label>女</label>
<input type="radio" value="2" name="gender" checked="checked" />
</form>
</body>
</html>
二、下拉框
下拉框可以有效的节省网页空间。既可以单选、又可以多选。语法规则为:
<option value=’提交值’selected=”selected”>选项</option>
提交值是指向服务器提交的值,选项是指显示的值。如果某个选项中使用selected说明该选项被默认选中。现在可以直接使用”selected”。
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>下拉列表框</title>
</head>
<body>
<form action="save.php" method="post" >
<label>爱好:</label>
<select>
<option value="看书">看书</option>
<option value="旅游" selected="selected">旅游</option>
<option value="运动">运动</option>
<option value="购物">购物</option>
</select>
</form>
</body>
</html>
如果想把下拉框的状态默认为空白,但是下拉菜单中没有空白选项的方法是:
<option value=" " selected="selected" id="test"> </option>
然后再head部分给test加个Css样式,让选项不显示,具体如下
<style>
#test{display:none;
}
</style>
具体代码为:
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>下拉列表框</title>
<style>
#test{display:none;
}
</style>
</head>
<body>
<form action="save.php" method="post" >
<label>爱好:</label>
<select>
<option value="null" selected="selected" id="test"></option>
<option value="看书">看书</option>
<option value="旅游">旅游</option>
<option value="运动">运动</option>
<option value="购物">购物</option>
</select>
</form>
</body>
</html>
三、下拉框的多选
下拉列表也可以进行多选操作,在<select>标签中设置multiple="multiple"属性,就可以实现多选功能,在 windows 操作系统下,进行多选时按下Ctrl键+单击(在 Mac下使用 Command +单击),可以选择多个选项。
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>下拉列表框</title>
</head>
<body>
<form action="save.php" method="post" >
<label>爱好:</label>
<select multiple=”multiple”>
<option value="看书">看书</option>
<option value="旅游" >旅游</option>
<option value="运动">运动</option>
<option value="购物">购物</option>
</select>
</form>
</body>
</html>
四、提交按钮
在表单中有两种按钮可以供使用:提交按钮、重置。用户需要提交表单信息到服务器时,需要用到提交按钮。语法为:
<input type=”submit” value=”提交” >
只有当type设置为submit时,按钮才会有提交作用;value按钮上显示的文字。
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>提交按钮</title>
</head>
<body>
<form method="post" action="save.php">
<label for="myName">姓名:</label>
<input type="text" value=" " name="myName " />
<input type="submit" value="提交" name="submitBtn" />
</form>
</body>
</html>
五、重置按钮
当用户需要重置表单信息到初始时的状态时,比如用户输入“用户名”后,发现书写有误,可以使用重置按钮使输入框恢复到初始状态。只需要把type设置为"reset"就可以。语法规则为:
<input type=”reset” value=”重置”>
只有当type设置为reset时,按钮才会有提交作用;value按钮上显示的文字。
六、label标签
label标签不会向用户呈现任何特殊效果,它的作用是为鼠标用户改进了可用性。如果你在 label 标签内点击文本,就会触发此控件。当用户单击选中该label标签时,浏览器就会自动将焦点转到和标签相关的表单控件上(就自动选中和该label标签相关连的表单控件上)。语法规则为:
<label for=”控件id名称”>
注意的是for属性中的值和相关控件中id属性的值要保持一致。例如:
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>form中的lable标签</title>
</head>
<body>
<form>
<label for="diaocha">你对什么运动感兴趣:</label>
<br />
<label for="tiyu">慢跑</label>
<input type="radio" name="running" id="tiyu" />
<br />
<label for="tiyu">登山</label>
<input type="radio" name="hiking" id="tiyu" />
<br />
<label for="tiyu">篮球</label>
<input type="radio" name="basketball" id="tiyu" />
</form>
</body>
</html>
将“radio”改为“checkbox”圆圈变成方框。