>>分享Web前端开发技术,并对孙卫琴的《精通Vue.js:Web前端开发技术详解》提供技术支持 书籍支持  卫琴直播  品书摘要  在线测试  资源下载  联系我们
发表一个新主题 开启一个新投票 回复文章 您是本文章第 20902 个阅读者 刷新本主题
 * 贴子主题:  HTML5 表单属性 回复文章 点赞(0)  收藏  
作者:flybird    发表时间:2020-01-30 23:45:18     消息  查看  搜索  好友  邮件  复制  引用

  

HTML5  表单属性

HTML5 新的表单属性

   HTML5 的 <form> 和 <input>标签添加了几个新属性.

    <form>新属性:    
  • autocomplete
  • novalidate
<input>新属性:    
  • autocomplete
  • autofocus
  • form
  • formaction
  • formenctype
  • formmethod
  • formnovalidate
  • formtarget
  • height 与 width
  • list
  • min 与 max
  • multiple
  • pattern (regexp)
  • placeholder
  • required
  • step

<form> / <input> autocomplete 属性

autocomplete 属性规定 form 或 input 域应该拥有自动完成功能。

    当用户在自动完成域中开始输入时,浏览器应该在该域中显示填写的选项。

    提示:  autocomplete 属性有可能在 form元素中是开启的,而在input元素中是关闭的。

     注意: autocomplete 适用于 <form> 标签,以及以下类型的 <input> 标签:text, search, url, telephone, email, password, datepickers, range 以及 color。

   点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小

范例

HTML form 中开启 autocomplete (一个 input 字段关闭 autocomplete ):


< form   action = " demo-form.php "   autocomplete = " on " >
  First name: < input   type = " text "   name = " fname " > < br >
  Last name:  < input   type = " text "   name = " lname " > < br >
  E-mail:  < input   type = " email "   name = " email "   autocomplete = " off " > < br >
   < input   type = " submit " >
</ form >

   提示:某些浏览器中,您可能需要启用自动完成功能,以使该属性生效。  

<form> novalidate 属性

   novalidate 属性是一个 boolean(布尔) 属性.

    novalidate 属性规定在提交表单时不应该验证 form 或 input 域。

   点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小

范例

无需验证提交的表单数据


< form   action = " demo-form.php "   novalidate >
  E-mail:  < input   type = " email "   name = " user_email " >
   < input   type = " submit " >
</ form >

<input> autofocus 属性

   autofocus 属性是一个 boolean 属性.

    autofocus 属性规定在页面加载时,域自动地获得焦点。

   点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小

范例

让 "First name" input 输入域在页面载入时自动聚焦:

First name: < input   type = " text "   name = " fname "   autofocus >

<input> form 属性

    form 属性规定输入域所属的一个或多个表单。

    提示:如需引用一个以上的表单,请使用空格分隔的列表。

   点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小

范例

位于form表单外的 input 字段引用了 HTML form (该 input 表单仍然属于form表单的一部分):


< form   action = " demo-form.php "   id = " form1 " >
  First name:  < input   type = " text "   name = " fname " > < br >
   < input   type = " submit "   value = " 提交 " >
</ form >

Last name:  < input   type = " text "   name = " lname "   form = " form1 " >

<input> formaction 属性

    The formaction 属性用于描述表单提交的URL地址.

    The formaction 属性会覆盖<form> 元素中的action属性.

    注意: The formaction 属性用于 type="submit" 和 type="image".

   点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小

范例

以下HTMLform表单包含了两个不同地址的提交按钮:


< form   action = " demo-form.php " >
  First name:  < input   type = " text "   name = " fname " > < br >
  Last name:  < input   type = " text "   name = " lname " > < br >
   < input   type = " submit "   value = " 提交 " > < br >
   < input   type = " submit "   formaction = " demo-admin.php "
   value = " 提交 " >
</ form >

<input> formenctype 属性

formenctype 属性描述了表单提交到服务器的数据编码 (只对form表单中 method="post" 表单)

    formenctype 属性覆盖 form 元素的 enctype 属性。

    主要: 该属性与 type="submit" 和 type="image" 配合使用。

   点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小

范例

第一个提交按钮已默认编码发送表单数据,第二个提交按钮以 "multipart/form-data" 编码格式发送表单数据:


< form   action = " demo-post_enctype.php "   method = " post " >
  First name:  < input   type = " text "   name = " fname " > < br >
   < input   type = " submit "   value = " 提交 " >
   < input   type = " submit "   formenctype = " multipart/form-data "
   value = " 以 Multipart/form-data 提交 " >
</ form >

<input> formmethod  属性

formmethod 属性定义了表单提交的方式。

formmethod 属性覆盖了 <form> 元素的 method 属性。

    注意: 该属性可以与 type="submit" 和 type="image" 配合使用。

   点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小

范例

重新定义表单提交方式范例:


< form   action = " demo-form.php "   method = " get " >
  First name:  < input   type = " text "   name = " fname " > < br >
  Last name:  < input   type = " text "   name = " lname " > < br >
   < input   type = " submit "   value = " 提交 " >
   < input   type = " submit "   formmethod = " post "   formaction = " demo-post.php "
   value = " 使用 POST 提交 " >
</ form >

<input> formnovalidate 属性

    novalidate 属性是一个 boolean 属性.

    novalidate属性描述了 <input> 元素在表单提交时无需被验证。

    formnovalidate 属性会覆盖 <form> 元素的novalidate属性.

     注意:  formnovalidate 属性与type="submit一起使用

   点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小

范例

两个提交按钮的表单(使用与不适用验证 ):


<form action="demo-form.php">

                 E-mail: <input type="email" name="userid"><br>

<input type="submit" value="提交"><br>

<input type="submit" formnovalidate value="不验证提交">

</form>

<input> formtarget 属性

   formtarget 属性指定一个名称或一个关键字来指明表单提交数据接收后的展示。

    The formtarget 属性覆盖 <form>元素的target属性.

    注意:  formtarget 属性与type="submit" 和 type="image"配合使用.

   点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小

范例

两个提交按钮的表单, 在不同窗口中显示:


<form action="demo-form.php">

First name: <input type="text" name="fname"><br>

Last name: <input type="text" name="lname"><br>

<input type="submit" value="正常提交">

                  <input type="submit" formtarget="_blank"       value="提交到一个新的页面上">

</form>

<input> height 和 width 属性

height 和 width 属性规定用于 image 类型的 <input> 标签的图像高度和宽度。

    注意: height 和 width 属性只适用于 image 类型的<input> 标签。

    提示:图像通常会同时指定高度和宽度属性。如果图像设置高度和宽度,图像所需的空间

在加载页时会被保留。如果没有这些属性,

浏览器不知道图像的大小,并不能预留

适当的空间。图片在加载过程中会使页面布局效果改变

(尽管图片已加载)。

   点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小

范例

定义了一个图像提交按钮, 使用了 height 和 width 属性:

<input type="image" src="img_submit.gif" alt="Submit" width="48"

height="48">

<input> list 属性

list 属性规定输入域的 datalist。datalist 是输入域的选项列表。

   点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小

范例

在 <datalist>中预定义 <input> 值:

     <input list="browsers">

     <datalist id="browsers">


    <option value="Internet Explorer">

    <option value="Firefox">

    <option value="Chrome">

    <option value="Opera">

    <option value="Safari">

    </datalist>

<input> min 和 max 属性

min、max 和 step 属性用于为包含数字或日期的 input 类型规定限定(约束)。

     注意: min、max 和 step 属性适用于以下类型的 <input> 标签:date pickers、number 以及 range。

   点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小

范例

<input> 元素最小值与最大值设置:

   Enter a date before 1980-01-01:

    <input type="date" name="bday" max="1979-12-31">

   Enter a date after 2000-01-01:

    <input type="date" name="bday" min="2000-01-02">

   Quantity (between 1 and 5):

    <input type="number" name="quantity" min="1" max="5">

<input> multiple 属性

multiple 属性是一个 boolean 属性.

    multiple 属性规定<input> 元素中可选择多个值。

    注意: multiple 属性适用于以下类型的 <input> 标签:email 和 file:

   点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小

范例

上传多个文件:

   Select images: <input type="file" name="img" multiple>

<input> pattern 属性

pattern 属性描述了一个正则表达式用于验证 <input> 元素的值。

    注意:pattern 属性适用于以下类型的 <input> 标签: text, search, url, tel, email, 和 password.

     提示: 是用来全局title属性描述了模式.

   点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小

范例

下面的例子显示了一个只能包含三个字母的文本域(不含数字及特殊字符):

   Country code: <input type="text" name="country_code" pattern="[A-Za-z]{3}"

title="Three letter country code">

<input> placeholder 属性

placeholder 属性提供一种提示(hint),描述输入域所期待的值。

    简短的提示在用户输入值前会显示在输入域上。

    注意: placeholder 属性适用于以下类型的 <input>  标签:text, search, url, telephone, email 以及 password。

   点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小

范例

input 字段提示文本t:

   <input type="text" name="fname" placeholder="First name">

<input> required 属性

required 属性是一个 boolean 属性.

    required 属性规定必须在提交之前填写输入域(不能为空)。

    注意:required 属性适用于以下类型的 <input> 标签:text, search, url, telephone, email, password, date pickers, number, checkbox, radio 以及 file。

   点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小

范例

不能为空的input字段:

   Username: <input type="text" name="usrname" required>

<input> step 属性

step 属性为输入域规定合法的数字间隔。

    如果 step="3",则合法的数是 -3,0,3,6 等

    提示:  step 属性可以与

max 和 min 属性创建一个区域值.

     注意:  step 属性与以下type类型一起使用: number, range, date, datetime, datetime-local, month, time 和 week.

   点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小点击在新窗口中浏览原图
CTRL+鼠标滚轮放大或缩小

范例

规定input step步长为3:

   <input type="number" name="points" step="3">

HTML5 <input> 标签


标签 描述
<form> 定义一个form表单
<input> 定义一个 input 域






程序猿的技术大观园:www.javathinker.net



[这个贴子最后由 admin 在 2020-02-01 16:50:56 重新编辑]
  Java面向对象编程-->输入与输出(下)
  JavaWeb开发-->JSP技术详解(Ⅱ)
  JSP与Hibernate开发-->使用JPA和注解
  Java网络编程-->通过JavaMail API收发邮件
  精通Spring-->绑定CSS样式
  Vue3开发-->计算属性和数据监听
  Promise对象的then()和catch()方法
  前端面试题及答案汇总
  Thinking In Vue:vue指令的封装
  Vue自定义指令的用法
  jQuery 添加元素
  JavaScript的HTML DOM td / th 对象
  HTML DOM Image图片对象
  JavaScript的Window 对象
  JavaScript Error(错误) 对象
  jQuery UI 如何使用部件库
  JavaScript的window.screen对象
  JavaScript中的 this关键字
  JavaScript 闭包
  JavaScript 对象 的创建和使用
  jQuery Mobile 简介
  更多...
 IPIP: 已设置保密
楼主      
1页 0条记录 当前第1
发表一个新主题 开启一个新投票 回复文章


中文版权所有: JavaThinker技术网站 Copyright 2016-2026 沪ICP备16029593号-2
荟萃Java程序员智慧的结晶,分享交流Java前沿技术。  联系我们
如有技术文章涉及侵权,请与本站管理员联系。