HTML 输入类型
HTML 输入类型有 text,password,submit,radio,checkbox,button,本章节着重讲解这些输入类型的用法。
输入类型:text
<input type="text"> 定义供文本输入的单行输入字段:
实例
<!DOCTYPE html><html><body><form action="/example/html/html_form.aspx">姓氏:<br><input type="text" name="firstname"><br>名字:<br><input type="text" name="lastname"><br><br><input type="submit"></form><p>请注意表单本身是不可见的。</p><p>同时请注意文本字段的默认宽度是 20 个字符。</p></body></html>
输入类型:password
<input type="password"> 定义密码字段。
实例
<!DOCTYPE html><html><body><form action="/example/html/html_form.aspx">用户名:<br><input type="text" name="userid"><br>密码:<br><input type="password" name="psw"></form><p>密码字段中的字符被掩码(显示为星号或圆点)。</p></body></html>
注释:password 字段中的字符会被做掩码处理(显示为星号或实心圆)。
输入类型:submit
<input type="submit"> 定义提交表单数据至表单处理程序的按钮。表单处理程序(form-handler)通常是包含处理输入数据的脚本的服务器页面。在表单的 action 属性中规定表单处理程序(form-handler):
实例
<!DOCTYPE html><html><body><form action="/example/html/html_form.aspx">姓氏:<br><input type="text" name="firstname" value="韩"><br>名字:<br><input type="text" name="lastname" value="梅梅"><br><br><input type="submit" value="Submit"></form><p>如果您点击提交,表单数据会被发送到名为 demo_form.ashx 的页面。</p></body></html>
以上 HTML 代码在浏览器中看上去是这样的:
姓氏:
名字:
如果您省略了提交按钮的 value 属性,那么该按钮将获得默认文本:
<!DOCTYPE html><html><body><form action="/example/html/html_form.aspx">姓氏:<br><input type="text" name="firstname" value="韩"><br>名字:<br><input type="text" name="lastname" value="梅梅"><br><br><input type="submit"></form></body></html>
Input Type: radio
<input type="radio"> 定义单选按钮。Radio buttons 是让用户在限定的选项中选择其中一个:
实例
<!DOCTYPE html><html><body><form action="/example/html/html_form.aspx"><input type="radio" name="sex" value="1" checked>男<br><input type="radio" name="sex" value="2">女<br><br><input type="submit"></form></body></html>
以上 HTML 代码在浏览器中看上去是这样的:
男
女
Input Type: checkbox
<input type="checkbox"> 定义复选框。复选框允许用户在有限数量的选项中选择零个或多个选项。
实例
<!DOCTYPE html><html><body><form action="/example/html/html_form.aspx"><input type="checkbox" name="vehicle" value="Bike">我有一辆自行车<br><input type="checkbox" name="vehicle" value="Car">我有一辆车<br><br><input type="submit"></form></body></html>
亲自试一试以上 HTML 代码在浏览器中看上去是这样的:
我有一辆自行车
我有一辆车
Input Type: button
<input type="button> 定义按钮。
实例
<!DOCTYPE html><html><body><button type="button" onclick="alert('Hello World!')">点击我!</button></body></html>
以上 HTML 代码在浏览器中看上去是这样的:
HTML5 输入类型
HTML5 增加了多个新的输入类型:
- color
- date
- datetime
- datetime-local
- month
- number
- range
- search
- tel
- time
- url
- week
注释:老式 web 浏览器不支持的输入类型,会被视为输入类型 text。
输入类型:number
<input type="number"> 用于应该包含数字值的输入字段。您能够对数字做出限制。根据浏览器支持,限制可应用到输入字段。
实例
<!DOCTYPE html><html><body><p>根据浏览器支持:<br>数值约束会应用到输入字段中。</p><form action="/example/html/html_form.aspx">数量(1 到 5 之间):<input type="number" name="quantity" min="1" max="5"><input type="submit"></form><p><b>注释:</b>IE9 及早期版本不支持 type="number"。</p></body></html>
输入限制
这里列出了一些常用的输入限制(其中一些是 HTML5 中新增的):
| 属性 | 描述 |
|---|---|
| disabled | 规定输入字段应该被禁用。 |
| max | 规定输入字段的最大值。 |
| maxlength | 规定输入字段的最大字符数。 |
| min | 规定输入字段的最小值。 |
| pattern | 规定通过其检查输入值的正则表达式。 |
| readonly | 规定输入字段为只读(无法修改)。 |
| required | 规定输入字段是必需的(必需填写)。 |
| size | 规定输入字段的宽度(以字符计)。 |
| step | 规定输入字段的合法数字间隔。 |
| value | 规定输入字段的默认值。 |
实例
<!DOCTYPE html><html><body><p>取决于浏览器支持:<br>固定步骤将应用于输入字段。</p><form action="/example/html/html_form.aspx">数量:<input type="number" name="points"min="0" max="100" step="10" value="30"><input type="submit"></form><p><b>Note:</b>type="number" 不支持IE9及更早的版本.</p></body></html>
输入类型:date
<input type="date"> 用于应该包含日期的输入字段。根据浏览器支持,日期选择器会出现输入字段中。
实例
<!DOCTYPE html><html><body><p>根据浏览器支持:<br>当您填写输入字段时会弹出日期选择器。</p><form action="/example/html/html_form.aspx">生日:<input type="date" name="bday"><input type="submit"></form><p><b>注释:</b>Firefox 或者Internet Explorer 11 以及更早版本不支持 type="date"。</p></body></html>
您可以向输入添加限制:
实例
<!DOCTYPE html><html><body><form action="/example/html/html_form.aspx">请输入 1980-01-01 之前的日期:<br><input type="date" name="bday" max="1979-12-31"><br><br>请输入 2000-01-01 之后的日期:<br><input type="date" name="bday" min="2000-01-02"><br><br><input type="submit"></form><p><b>注释:</b>Internet Explorer 不支持 type="date"。</p></body></html>
输入类型:color
<input type="color"> 用于应该包含颜色的输入字段。根据浏览器支持,颜色选择器会出现输入字段中。
实例
<!DOCTYPE html><html><body><p>取决于浏览器支持:<br>当您填写输入框时,会弹出一个颜色选择器。</p><form action="/example/html/html_form.aspx">选择你喜欢的颜色:<input type="color" name="favcolor" value="#ff0000"><input type="submit"></form><p><b>Note:</b> type="color" Internet Explorer不支持.</p></body></html>
输入类型:range
<input type="range"> 用于应该包含一定范围内的值的输入字段。根据浏览器支持,输入字段能够显示为滑块控件。
实例
<!DOCTYPE html><html><body><p>根据浏览器支持:<br>输入类型 "range" 可显示为滑动控件。</p><form action="/example/html/html_form.aspx" method="get">Points:<input type="range" name="points" min="0" max="10"><input type="submit"></form><p><b>注释:</b>IE9 及早期版本不支持 type="range"。</p></body></html>
您能够使用如下属性来规定限制:min、max、step、value。
输入类型:month
<input type="month"> 允许用户选择月份和年份。根据浏览器支持,日期选择器会出现输入字段中。
实例
<!DOCTYPE html><html><body><p>根据浏览器支持:<br>当您填写输入字段时会弹出日期选择器。</p><form action="/example/html/html_form.aspx">生日(月和年):<input type="month" name="bdaymonth"><input type="submit"></form><p><b>注释:</b>Firefox 或者Internet Explorer 11 以及更早版本不支持 type="month"。</p></body></html>
输入类型:week
<input type="week"> 允许用户选择周和年。根据浏览器支持,日期选择器会出现输入字段中。
实例
<!DOCTYPE html><html><body><p>根据浏览器支持:<br>当您填写输入字段时会弹出日期选择器。</p><form action="/example/html/html_form.aspx">选择一个周:<input type="week" name="year_week"><input type="submit"></form><p><b>注释:</b>Internet Explorer 不支持 type="week"。</p></body></html>
输入类型:time
<input type="time"> 允许用户选择时间(无时区)。根据浏览器支持,时间选择器会出现输入字段中。
实例
<!DOCTYPE html><html><body><p>根据浏览器支持:<br>当您填写输入字段时会弹出日期选择器。</p><form action="/example/html/html_form.aspx">请选取一个时间:<input type="time" name="usr_time"><input type="submit"></form><p><b>注释:</b>Firefox 或者Internet Explorer 11 以及更早版本不支持 type="time"。</p></body></html>
输入类型:datetime
<input type="datetime"> 允许用户选择日期和时间(有时区)。根据浏览器支持,日期选择器会出现输入字段中。
实例
<!DOCTYPE html><html><body><p>根据浏览器支持:<br>当您填写输入字段时会弹出日期选择器。</p><form action="/example/html/html_form.aspx">生日(日期和时间):<input type="datetime" name="bdaytime"><input type="submit"></form><p><b>注释:</b>Chrome、Firefox 或 Internet Explorer 不支持 type="datetime"。</p></body></html>
输入类型:datetime-local
<input type="datetime-local"> 允许用户选择日期和时间(无时区)。根据浏览器支持,日期选择器会出现输入字段中。
实例
<!DOCTYPE html><html><body><p>根据浏览器支持:<br>当您填写输入字段时会弹出日期选择器。</p><form action="/example/html/html_form.aspx">生日(日期和时间):<input type="datetime-local" name="bdaytime"><input type="submit" value="Send"></form><p><b>注释:</b>Firefox 或者Internet Explorer 不支持 type="datetime-local"。</p></body></html>
输入类型:email
<input type="email"> 用于应该包含电子邮件地址的输入字段。根据浏览器支持,能够在被提交时自动对电子邮件地址进行验证。某些智能手机会识别 email 类型,并在键盘增加 ".com" 以匹配电子邮件输入。
实例
<!DOCTYPE html><html><body><form action="/example/html/html_form.aspx">E-mail:<input type="email" name="email"><input type="submit"></form><p><b>注释:</b>IE9 及更早版本不支持 type="email"。</p></body></html>
输入类型:search
<input type="search"> 用于搜索字段(搜索字段的表现类似常规文本字段)。
实例
<!DOCTYPE html><html><body><form action="/example/html/html_form.aspx">搜索谷歌:<input type="search" name="googlesearch"><input type="submit"></form></body></html>
输入类型:tel
<input type="tel"> 用于应该包含电话号码的输入字段。目前只有 Safari 8 支持 tel 类型。
实例
<!DOCTYPE html><html><body><form action="/example/html/html_form.aspx">Telephone:<input type="tel" name="usrtel"><input type="submit"></form><p><b>注释:</b>Safari 8 及更新版本支持 type="tel"。</p></body></html>
输入类型:url
<input type="url"> 用于应该包含 URL 地址的输入字段。根据浏览器支持,在提交时能够自动验证 url 字段。某些智能手机识别 url 类型,并向键盘添加 ".com" 以匹配 url 输入。
实例
<!DOCTYPE html><html><body><form action="/example/html/html_form.aspx">请添加您的首页:<input type="url" name="homepage"><input type="submit"></form><p><b>Note:</b>IE9 及其更早版本不支持 type="url"。</p></body></html>