菜单
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

HTML <input> 标签


示例

一个带有三个输入字段的 HTML 表单;两个文本字段和一个提交按钮

<form action="/action_page.php">
  <label for="fname">名:</label>
  <input type="text" id="fname" name="fname"><br><br>
  <label for="lname">姓氏:</label>
  <input type="text" id="lname" name="lname"><br><br>
  <input type="submit" value="提交">
</form>
自己动手试一试 »

定义和用法

HTML <input> 标签用于指定用户可以输入数据的输入字段。

<input> 元素是最重要的表单元素。

<input> 元素可以显示多种方式,具体取决于 type 属性。

不同的输入类型如下:

  • <input type="button">
  • <input type="checkbox">
  • <input type="color">
  • <input type="date">
  • <input type="datetime-local">
  • <input type="email">
  • <input type="file">
  • <input type="hidden">
  • <input type="image">
  • <input type="month">
  • <input type="number">
  • <input type="password">
  • <input type="radio">
  • <input type="range">
  • <input type="reset">
  • <input type="search">
  • <input type="submit">
  • <input type="tel">
  • <input type="text">(默认值)
  • <input type="time">
  • <input type="url">
  • <input type="week">

查看 type 属性,了解每种输入类型的示例!


提示和注释

提示:始终使用 <label> 标签为 <input type="text"><input type="checkbox"><input type="radio"><input type="file"><input type="password"> 定义标签。


浏览器支持

元素
<input>


属性

Attribute 描述
accept file_extension
audio/*
video/*
image/*
media_type
指定用户在文件选择对话框中可以选择的文件类型的过滤器(仅适用于 type="file")
alt text 为图像指定替代文本(仅适用于 type="image")
autocomplete on
off
指定是否启用 <input> 元素的自动完成功能
autofocus autofocus 指定页面加载时 <input> 元素应自动获得焦点
checked checked 指定页面加载时 <input> 元素应预先选中(适用于 type="checkbox" 或 type="radio")
dirname inputname.dir 指定将提交文本方向
disabled disabled 指定 <input> 元素应被禁用
form form_id 指定 <input> 元素所属的表单
formaction URL 指定提交表单时表单数据应如何编码(适用于 type="submit" 和 type="image")
formenctype application/x-www-form-urlencoded
multipart/form-data
text/plain
指定提交表单时表单数据应如何编码(适用于 type="submit" 和 type="image")
formmethod get
post
定义提交到 action URL 的数据的 HTTP 方法(适用于 type="submit" 和 type="image")
formnovalidate formnovalidate 定义提交时表单元素不应进行验证
formtarget _blank
_self
_parent
_top
framename
指定提交表单后收到的响应应显示的位置(适用于 type="submit" 和 type="image")
height 像素 <input> 元素的 height(仅适用于 type="image")
列表 datalist_id 引用包含 <input> 元素的预定义选项的 <datalist> 元素
max 数字
date
指定 <input> 元素的最大值
maxlength 数字 指定 <input> 元素允许的最大字符数
分钟 数字
date
指定 <input> 元素的最小值
minlength 数字 指定 <input> 元素所需的最小字符数
multiple multiple 指定用户可以在 <input> 元素中输入多个值
name text 指定 <input> 元素的名称
pattern regexp 指定 <input> 元素的值要检查的正则表达式
placeholder text 指定描述 <input> 元素预期值的简短提示
popovertarget element_id 指定要调用的弹出式元素(仅适用于 type="button")
popovertargetaction hide
show
toggle
指定点击按钮时弹出式元素会发生什么(仅适用于 type="button")
readonly readonly 指定输入字段是否为只读
required required 指定在提交表单之前必须填写输入字段
大小 数字 <input> 元素的宽度(以字符为单位)
src URL 指定用作提交按钮的图像的 URL(仅适用于 type="image")
step number
any
指定输入字段中合法数字的间隔
type button
checkbox
color
date
datetime-local
电子邮件
file
hidden
图片
month
数字
password
收音机
range
reset
search
submit
tel
text
time
url
week
指定要显示的 <input> 元素类型
value text 指定 <input> 元素的值
 
width 像素 <input> 元素的 width(仅适用于 type="image")

全局属性

<input> 标签也支持 HTML 中的全局属性


事件属性

<input> 标签也支持 HTML 中的事件属性


相关页面

HTML 教程

HTML DOM 参考


默认 CSS 设置

无。


×

联系销售

如果您想将 W3Schools 服务用于教育机构、团队或企业,请发送电子邮件给我们
sales@w3schools.com

报告错误

如果您想报告错误,或想提出建议,请发送电子邮件给我们
help@w3schools.com

W3Schools 经过优化,旨在方便学习和培训。示例可能经过简化,以提高阅读和学习体验。教程、参考资料和示例会不断审查,以避免错误,但我们无法保证所有内容的完全正确性。使用 W3Schools 即表示您已阅读并接受我们的使用条款Cookie 和隐私政策

版权所有 1999-2024 Refsnes Data。保留所有权利。W3Schools 由 W3.CSS 提供支持