HTML5-定制input元素

下述内容主要讲述了《HTML5权威指南》第13章关于“定制input元素”。

input元素可以生成一个供用户输入数据的简单文本框。其缺点在于用户在其中输入什么值都可以,可以配置type类型来获取额外的属性。其中type属性有23个不同的值,而input元素共有30个属性,其中许多属性只能与特定的type属性值搭配使用。

一、用input元素输入文字

type属性设置为text的input元素在浏览器中显示为一个单行文本框。

1. 设定元素大小

maxlength属性设定用户能够输入的字符的最大数目;

size属性设定文本框能够显示的字符数目。

<label for="username"></label>
<input type="text" name="username" id="username" maxlength="10" size="20">

2. 设置初始值和占位式提示

value属性设定默认值;

placeholder属性设置一段提示文字,告知用户输入什么类型的数据。

<label for="address"></label>
<input type="text" name="address" id="address" value="北京市">
<label for="tel"></label>
<input type="text" name="telephone" placeholder="请输入电话号码">

3. 使用数据列表

可以将input元素的list属性设置为一个datalist元素的id属性值,这样用户在文本框中输入数据时只需从后一元素提供的一批选项中选择就行了。

<input type="text" name="city" list="cityList">
<datalist id="cityList">
    <option value="BeiJing" label="北京市"></option>
    <option value="QingDao">青岛市</option>
    <option value="YanTai"></option>
</datalist>

图 chrome下展示

图 firefox下展示

注意:在不同浏览器下表现会有所不同

(1)datalist元素中的每一个option元素都代表一个供用户选择的值,其value属性值在该元素代表的选项被选中时就是input元素所用的数据值;

(2)说明信息,可以通过label属性设置,也可以作为option元素的内容设置。

4. 生成只读或被禁用的文本框

readonly和disabled属性都可以用来生成用户不能编辑的文本框,其结果的外观不同。

<input type="text" name="readonly" value="readonly" readonly>
<input type="text" name="disabled" value="disabled" disabled>

注意:设置了disabled属性的input元素的数据不能被提交到服务器;readonly属性的input元素的数据可以被提交到服务器

建议:readonly属性需要谨慎使用(无视觉信号告知用户禁止编辑,用户不能输入,让用户困惑),应该考虑使用hidden类型的input元素;

二、用input元素为输入数据把关

1. 用input元素获取数值

type属性设置为number的input元素生成的输入框只允许接受数值。

  • min设定可接受的最小值;
  • max设定可接受的最大值;
  • step指定上下调节数值的步长。
<fieldset>
    <legend>number</legend>
    <label for="score">分数:</label>
    <input type="number" name="score" id="score" min="60" max="100" step="5">
</fieldset>

2. 用input元素获取指定范围内的数值

range型input元素可以事先规定一个数值范围供用户选择。

<fieldset>
    <legend>range</legend>
    <label for="price">价格:</label>
    <span>1</span>
    <input type="range" name="price" id="price" min="0" max="100" step="5">
    <span>100</span>
</fieldset>

3. 用input元素获取布尔型输入

checkbox型input会生成供用户选择是或否的复选框。

<fieldset>
    <legend>checkbox</legend>
    <input type="checkbox" name="agree" id="agree">
    <label for="agree">同意条款</label>
</fieldset>

注意:提交表单时,只有处于勾选状态的复选框的数据值会发送给服务器(checkbox型input元素的数据项如果不存在于提交项中,则表明用户未勾选)。

4. 用input元素生成一组固定选项

radio型input元素用来生成一组单选按钮,供用户从一批固定的选项中作出选择。它适合于可用有效数据不多的情况。要生成一组互斥的选项,只需将所有相关input元素的name属性设置为同一个值即可。

<fieldset>
    <legend>选出你最喜欢的水果:</legend>
    <label for="oranges">
        <input type="radio" value="oranges" id="oranges" name="fave">
        Oranges
    </label>
    <label for="apples">
        <input type="radio" value="apples" id="apples" name="fave" checked>
        Apples
    </label>
</fieldset>

5. 用input元素获取有规定格式的字符串

type属性设置为email、tel、url的input元素能够接受的输入数据分别为有效的电子邮箱地址、电话号码和URL。

<fieldset>
    <legend>规定格式的字符串</legend>
    <p>
        <label for="email">
            邮箱:<input type="email" name="email" id="email">
        </label>
    </p>
    <p>
        <label for="password">
            密码:<input type="password" name="password" id="password">
        </label>
    </p>
    <p>
        <label for="tel">
            电话:<input type="tel" name="tel" id="tel">
        </label>
    </p>
    <p>
        <label for="url">
            URL:<input type="text" name="url" id="url">
        </label>
    </p>
</fieldset>

注意:上述类型input元素,只有在提交表单的时候才会检测用户输入的数据,且检查效果各不相同。

6. 用input元素获取时间和日期

type属性值 说明 示例(chrome下)
datetime 获取世界时日期和时间,包括时区信息 根据填写情况
datetime-local 获取本地日期和时间,不包括时区信息 根据填写情况
date 获取本地日期 2016-08-12
month 获取年月信息 2016-08
time 获取时间 13:00
week 获取当前星期 2016-W32
<fieldset>
    <legend>日期&时间</legend>
    <p>
        <label for="myDateTime">
            日期时间:<input type="datetime" name="myDateTime" id="myDateTime">
        </label>
    </p>
    <p>
        <label for="myTime">
            时间:<input type="time" name="myTime" id="myTime">
        </label>
    </p>
    <p>
        <label for="myDate">
            日期:<input type="date" name="myDate" id="myDate">
        </label>
    </p>
    <p>
        <label for="myMonth">
            月份:<input type="month" name="myMonth" id="myMonth">
        </label>
    </p>
    <p>
        <label for="myWeek">
            周:<input type="week" name="myWeek" id="myWeek">
        </label>
    </p>
</fieldset>

7. 用input元素获取颜色值

color型input元素只能用来选择颜色,提交到服务器的7个字符,如”#011993”。

<fieldset>
    <legend>颜色</legend>
    <label for="color">
        颜色:<input type="color" name="color" id="color">
    </label>
</fieldset>

8. 用input元素生成图像按钮和分区响应图

image型input元素生成的按钮显示为一幅图像,点击它可以提交表单。

注意:在发送的数据中包括来自那个image型input元素的两个数据项,它们分别代表用户点击位置相对于图像左上角的x坐标和y坐标。

9. 用input元素上传文件

input元素类型是file型,它可以在提交表单时将文件上传到服务器。

属性 说明
accept 指定接受的MIME类型
multipe 设置这个属性的input元素可一次上传多个文件
<form action="http://localhost:8888/form/uploadFile" method="post" enctype="multipart/form-data">
    <label for="filedata">请选择文件:</label>
    <input type="file" name="filedata" id="filedata">
    <button type="submit">提交</button>
</form>

注意:表单编码类型为multipart/form-data的时候才能上传文件。

时间: 2025-01-04 10:47:06

HTML5-定制input元素的相关文章

html5权威指南:定制input元素

第十三章:定制Inpur元素,http://www.cnblogs.com/polk6/p/5417921.html#Menu3-New input标签最全面的type属性:http://blog.sina.com.cn/s/blog_161005e400102wa2n.html

HTML5 改良的 input 元素的种类

在HTML5中,增加与改良的input元素中的type属性,包括url.email.date.time.datetime.datetime-local.month.week.number.range.search.tel.color类型. <!DOCTYPE html> <html> <head lang="en"> <title>增加与改良的input元素中的type属性</title> <meta charset=&

Html5 &lt;datalist&gt; input自动完成标签

用原生HTML5控件实现输入框自动提示(下拉列表) <datalist> 标签规定了 <input> 元素可能的选项列表. <datalist> 标签被用来在为 <input> 元素提供"自动完成"的特性. 用户能看到一个下拉列表,里边的选项是预先定义好的,将作为用户的输入数据. 请使用 <input> 元素的 list 属性来绑定 <datalist> 元素. 例子: <input list="b

(转)日期类型的input元素设置默认值为当天

原文地址 html5的form元素对日期时间有丰富的支持 <input type="date"> <input type="time"> <input type="datetime"> <input type="month"> <input type="week"> <input type="datetime-local"

检测浏览器对HTML5新input类型的支持

HTML5新增加了很多input元素类型,比如color,date,datetime,datetime-local,email,month,number,range,search,tel,time,url,week等. 通过以下方法可以检测浏览器是否支持这些新的input类型: var i = document.createElement('input'); i.setAttribute('type', 'date'); //浏览器不支持date类型 if(i.type == 'text'){

html5——表单元素和属性

html使用表单向服务器提交请求,表单控件的主要作用就是收集用户的输入,当用户提交表单时,用户输入内容将被作为请求参数提交到远程服务器上 html原有的表单及表单控件 form属性 说明 action 指定当单击表单的确认按钮,该表单被提交到哪个地址,可以是相对/绝对地址 method 指定提交表单时发送何种类型的请求 属性值可以为get post enctype 对表单内容进行编码所使用的字符集 name 指定表单的唯一名称,建议该属性值与id属性值保持一致 target 使用哪种方式打开目标

08 H5新增input元素

<!doctype html> <html> <head> <meta charset="utf-8"> <title>HTML5</title> </head> <body> 在android和ios中尤其能体现这些输入控件的特点,会自动切换到不同的虚拟键盘! <hr> <form> 查询文本框 <input type="search" p

HTML5 新模块元素兼容问题

新增块元素默认样式 下列HTML5新模块元素在IE8.9版本浏览器中没有被定义默认样式.为解决该问题,给下列元素添加“block”显示属性. 代码: article, aside, details, figcaption, figure, footer, header, hgroup, main, nav, section, summary { display: block; } 特殊新增块元素 下列HTML5新模块元素在IE8.9版本浏览器中没有被定义默认样式,考虑到其特殊性,特为下列元素添加

有关图片上传的相关知识input type=file,HTML5的 input:file上传类型控制

遇到项目,要求做一个影像系统,对于前端开发需要了解file的相关属性,以及如何开发.工欲善其事,必先利器嘛.度娘一阵子搜索,找资料.这年头,需要的是你解决问题的能力啊! 参考应用:https://www.cnblogs.com/sunliyuan/p/5737928.html http://blog.okbase.net/jquery2000/archive/1296.html(解释的也很清楚) http://blog.csdn.net/qingyjl/article/details/52003