HTML表单(form)
表单是可以把浏览者输入的数据传送到服务器端,然后服务器端程序
语法:
<form method="传送方式" action="服务器文件">
文本输入框、密码输入框
当用户要在表单中键入字母、数字等内容时,就会用到文本输入框。文本框也可以转化为密码输入框。
<form>
<input type="text/password" name="名称" value="文本" />
</form>
1
、type:
当type="text"时,输入框为文本
输入框;
当type="password"时,
输入框为密码输入框。
2
、name:
为文本框命名,以备后台程序ASP 、PHP使用。
3
、value:
为文本输入框设置默认值。(一般起到提示作用)
文本域,支持多行文本输入
语法:
<textarea rows="行数" cols="列数">文本</textarea>
rows cols 这两个属性可用css样式的width和height来代替:col用width、row用height来代替。
例:
<form method="post" action="save.php">
<label>个人简介</label>
<textarea cols="50" rows="10" >在这里输入内容...</textarea>
</form>
单选框、复选框
语法:
<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" 时,该选项被默认选中
如下面代码:
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv= "content -Type" content="text/html;charset=urf-8">
<title> radio checker</title>
</head>
<body>
<form method="post" action="save.php">
<label>性别:</label>
<label> 男</label>
<input type ="radio" value="1" name="text" />
<label> 女</label>
<input type ="radio" value="2" name="text" /> <br/>
你喜欢居住在哪里?<br/>
<input type="checkbox" value="北京" name="text1" />北京
<input type="checkbox" value="上海" name="text2" />上海
<input type="checkbox" value="广州" name="text2" checked="checked" />广州
</form>
</body>
</html>
结果 :
单选框 name 必须一样,才能有单选的效果
下拉列表框
语法:
<select>
<option value =" 提交值" > 选项 </ option>
<option value =" 向服务器提交的值" > 显示的值</ option>
<option value =" 音乐" > 音乐 </ option>
<option value ="舞蹈" >舞蹈</ option>
<option value ="阅读">阅读</option>
<option value ="旅游">旅游</option>
</select>
设置selected="selected"属性,则该选项就被默认选中。
使用下拉列表框进行多选
在<select>标签中设置multiple="multiple"
属性,就可以实现多选功能
在 windows 操作系统下,需要配合 Ctrl
键 完成多选(在 Mac下使用 Command +单击)
按钮
表单中有两种按钮可以使用,分别为:提交按钮、重置
语法:
<input type="submit/reset" value="提交/重置">value:按钮上显示的文字
label标签
label标签不会向用户呈现任何特殊效果,在 label 标签内点击文本,就会触发此控件。就是说,当用户单击选中该label标签时,浏览器就会自动将焦点转到和标签相关的表单控件上(就自动选中和该label标签相关连的表单控件上)。
语法:
<label for="控件id名称">
注意:标签的 for 属性中的值应当与相关控件的 id 属性值一定要相同。
- 所有表单控件(文本框、文本域、按钮、单选框、复选框等)都必须放在 <form></form> 标签之间(否则用户输入的信息可提交不到服务器上!)。
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv= "content -Type" content="text/html;charset=urf-8">
<title> 一个栗子</title>
</head>
<body>
<form method="post" action="save.php">
<label for="username">用户名:</label>
<input type="text" name="username" id="username" value="" />
<label for="pass">密  码:</label>
<input type="password" name="pass" id="pass" value="" />
<input type="submit" value="确定" name="submit" />
<input type="reset" value="重置" name="reset" /> <br/>
<label>性别:</label>
<label> 男</label>
<input type ="radio" value="1" name="text" />
<label> 女</label>
<input type ="radio" value="2" name="text" /> <br/>
你喜欢什么?<br/>
<select multiple="multiple">
<option value =" 音乐" > 音乐 </ option>
<option value ="舞蹈" >舞蹈</ option>
<option value ="阅读">阅读</option>
<option value ="旅游">旅游</option>
</select>
</form>
</body>
</html>
结果:
。。。。。。。。。。。。。。。
。。。。。。。。。。。。。。。。
。。。。。。。。。。。。。。。。。。。。。