众所周知,在web编程中,表单主要用于收集用户输入的数据。HTML5在保留原有HTML表单控件,属性的基础上,大大增强了表单,表单控件的功能。下面我将介绍一些HTML5表单及其控件的知识。
除此之外,html5还新增了一些属性。
<!DOCTYPE html> <html><head></head> <body> <form action="#" method="get"> <label>单行文本框:<input id="username" name="ursename" type="text"/></label><br/> 不能编辑的文本框:<input id="username2" name="username2" type="text" readonly="readonly" value="abc"/><br/> <label for="password">密码框:</label> <input id="password" name="password" type="password"/><br/> 隐藏框:<input id="hidden" name="hidden" type="hidden" value="123"/><br/> 第一组单选框:<br/> 红:<input id="red" name="color" type="radio" value="red"/> 绿:<input id="green" name="color" type="radio" value="green"/> 蓝:<input id="blue" name="color" type="radio" value="blue"/><br/> 两个复选框:<br/> leegang.org:<input id="a" name="website" type="checkbox" value="leegang.org"/> crazyit.org:<input id="b" name="website" type="checkbox" value="crazyit.org"/><br/> 文件上传框:<input id="file" name="file" type="file"/><br/> 图像域:<input type="image" src="images/logo.png" alt="上传图像失败" width="97" height="61"/><br/> 下面是四个按钮:<br/> <input id="ok" name="ok" type="submit" value="提交"/> <input id="dis" name="dis" type="submit" value="提交" disabled/> <input id="cancle" name="cancle" type="reset" value="重置"/> <input id="no" name="no" type="button" value="无动作"/> </form>
当然input元素也存在一些属性,像checked,disabled等,在这里就不一一介绍了。如果想要进一步了解,可以参考菜鸟教程http://www.runoob.com/html/html-forms.html来加深理解。
另外HTML5也增加了一些属性及元素:
<form action="#" method="post"> color文本框:<input type="color" name="color" onchange="a.value=this.value"/> <output name="a" for="color"></output><br/> date文本框:<input type="date" name="date"/><br/> time文本框:<input type="time" name="time"/><br/> datetime-local文本框:<input type="datetime-local" name="datetime-local"/><br/> month文本框:<input type="month" name="month"/><br/> week文本框:<input type="week" name="week"/><br/> email文本框:<input type="email" name="email"/><br/> tel文本框:<input type="tel" name="tel"/><br/> url文本框:<input type="url" name="url"/><br/> number文本框:<input type="number" name="number" min="0" max="100" step="5"/><br/> range文本框:0<input type="range" name="range" min="0" max="100" step="5" onchange="b.value=this.value"/>100 <output name="b" for="range"></output><br/> search文本框:<input type="search" name="search" required pattern="\d{3}-\d-\d{5}"/><br/>
有如下代码:
<form action="#" method="get"> <!--隐式关联--> <label for"username">单行文本框:</label> <input id="username" name="username" type="text"/><br/> <!--显示关联--> <label>密码框: <intout id="password" type="password"/></label> </form>
<form action="#" method="get"> <button type="submit">提交</button> <button type="reset">重置</button> </form>
<form action="#" method="get"> 下面是简单的下拉菜单:<br/> <select id="skills" name="skills"> <option value="java">Java语言</option> <option value="c"selected>C语言</option> <option value="ruby">ruby语言</option> </select><hr/> 下面是允许多选的列表框:<br/> <select id="books" name="books" size="4" multiple> <option value="java">Java疯狂讲义</option> <option value="and">and讲义</option> <option value="ee">ee讲义</option> </select> <hr/> 下面是允许多选的列表框:<br/> <select id="leegang" name="leegang" size="6" multiple> <optgroup label="疯狂Java图书"> <option value="java">java讲义</option> <option value="and">and讲义</option> <option value="ruby">ruby讲义</option> </optgroup> <optgroup label="其他图书"> <option value="s">权威指南</option> <option value="ror">ror讲义</option></optgroup> </select><br/> <button type="submit">提交</button> </form>
<form action="#" method="get"> <fieldset name="basic"> <legend>基本信息</legend> <label id="nameLD" for="username">用户名:</label> <input id="username" name="username" type="text" autofocus placeholder="请输入用户名"/><br/> <label>密码:<input id="password" name="password" type="password" placeholder="请输入密码"/></label> </fieldset> <fieldset name="exter"> <legend>附加信息</legend> <label>身高:<input id="height" name="height" type="text"/><br/></label> <label>出生地:<input id="birth" name="birth" type="text" list="bir"/><br/></label> <label>所在学校:<input id="school" name="school" type="text"/><br/></label> </fieldset> <button type="submit">提交</button>
range文本框:0<input type="range" name="range" min="0" max="100" step="5" onchange="b.value=this.value"/>100 <output name="b" for="range"></output>
行车速度:0<meter name="speed" value="20" min="0" max="200" low="20" high="160"></meter>200千米/小时<br/> 任务完成比:0<progress value="30" max="100">30/100</progress>100<br/>
这基本上就是对HTML5中form元素的介绍了,如果想要了解更多的HTML5知识,可以访问菜鸟教程http://www.runoob.com/html/html5-intro.html