form
功能:
表单用于向服务器传输数据,从而实现用户与Web服务器的交互
表单能够包含input系列标签,比如文本字段、复选框、单选框、提交按钮等等。
表单还可以包含textarea、select、fieldset和 label标签。
表单属性
属性 | 值 | 含义 |
---|---|---|
action | URL | 指定一个表单处理目标URL,表单数据将被提交到该URL地址的处理程序。如果该属性值为空,则提交到文档自身。 |
method | get或post | 将表单数据提交到http服务器的方法,默认为get |
enctype | application/x-www-form-urlencoded | 指定表单数据的编码类型,此属性只有在method属性设置为post时才有效。默认值为application/x-www-form-urlencode对所有字符进行编码。如果表单包含用于文件上传的控件(input type="file"),那么这个属性值必须设置为multipart/form-data,不对字符进行编码。 |
表单元素
基本概念:
HTML表单是HTML元素中较为复杂的部分,表单往往和脚本、动态页面、数据处理等功能相结合,因此它是制作动态网站很重要的内容。
表单一般用来收集用户的输入信息
表单工作原理:
访问者在浏览有表单的网页时,可填写必需的信息,然后按某个按钮提交。这些信息通过Internet传送到服务器上。
服务器上专门的程序对这些数据进行处理,如果有错误会返回错误信息,并要求纠正错误。当数据完整无误后,服务器反馈一个输入完成的信息
input系列
type属性值 | 表现形式 | 对应代码 |
---|---|---|
text | 单行输入文本 | <input type=text" /> |
password | 密码输入框 | <input type="password" /> |
checkbox | 复选框 | <input type="checkbox" checked="checked" /> |
radio | 单选框 | <input type="radio" /> |
submit | 提交按钮 | <input type="submit" value="提交" /> |
reset | 重置按钮 | <input type="reset" value="重置" /> |
button | 普通按钮 | <input type="button" value="普通按钮" /> |
hidden | 隐藏按钮 | <input type="hidden" value="隐藏按钮" /> |
file | 文本选择框 | <input type="file" /> |
类型(type)说明:
- text:文本输入框
- password:密码输入框
- radio:单选框
- checkbox:多选框
- submit:提交按钮
- button:可点击的按钮,没有任何行为,一般配合JS使用
- hidden:对用户不可见,通常用于修改某条数据时,记录数据的id值
- file:提交文件 form表单需要加上enctype="multipart/form-data",method="post"
属性说明:
- name:表单提交时的“键”,注意和id的区别
- value:表单提交时对应项的值
- type="button", "reset", "submit"时,为按钮上显示的文本年内容
- type="text","password","hidden"时,为输入框的初始值
- type="checkbox", "radio", "file",为输入相关联的值
- checked:radio和checkbox默认被选中的项
- readonly:text和password设置只读
- disabled:所有input均适用
select标签
<form action="" method="post">
<select name="city" id="city">
<option value="1">北京</option>
<option selected="selected" value="2">上海</option>
<option value="3">广州</option>
<option value="4">深圳</option>
</select>
</form>
属性说明:
- multiple:布尔属性,设置后为多选,否则默认单选
- disabled:禁用
- selected:默认选中该项
- value:定义提交时的选项值
label标签
定义:<label> 标签为 input 元素定义标注(标记)。
说明:
- label 元素不会向用户呈现任何特殊效果。
- <label> 标签的 for 属性值应当与相关元素的 id 属性值相同。
<form action="">
<label for="username">用户名</label>
<input type="text" id="username" name="username">
</form>
textarea多行文本
<textarea name="memo" id="memo" cols="30" rows="10">
默认内容
</textarea>
属性说明:
- name:名称
- rows:行数
- cols:列数
- disabled:禁用
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>form表单使用</title>
</head>
<body>
<form action="http://www.baidu.com">
<p>
<label for="t1">账号</label>
<input id="t1" type="text">
</p>
<p>
<label for="p1">密码</label>
<input id="p1" type="password">
</p>
<p>
<input type="submit" value="登录">
</p>
<p>喜欢的运动:
<label for="ch1">足球</label>
<input id="ch1" name="sports" type="checkbox">
<label for="ch2">蓝球</label>
<input id="ch2" name="sports" type="checkbox">
<label for="ch3">排球</label>
<input id="ch3" name="sports" type="checkbox">
</p>
<p>
<label for="ra1">足球</label>
<input id="ra1" name="sports" type="radio">
<label for="ra2">蓝球</label>
<input id="ra2" name="sports" type="radio">
<label for="ra3">排球</label>
<input id="ra3" name="sports" type="radio">
</p>
<p>
<label for="city">所在城市</label>
<select name="city" id="city">
<option value="1">石家庄</option>
<option value="2">邢台</option>
<option value="3">秦皇岛</option>
</select>
</p>
</form>
</body>
</html>