表单是HTML中获取用户输入的手段,HTML5对表单系统做了彻底的改造,以适应当前的应用。在HTML5中增加了从用户收集特定类型数据的新方法和在浏览器中检查数据的能力,但在使用有些新增特性前最好先检查一下它是否已经得到了广泛支持。
<form method="post" action="http://titan:8080/form"> <input name="fave"/> <button>Submit Vote</button> </form>该表单非常简单,就是一个输入框和一个提交按钮,但给了我们一个大概的轮廓,在此基础上进行扩充,就能构造出更有意义、更有用处的表单。下面是对这些表单元素及其属性的一个简单介绍。
<form method="post" action="http://titan:8080/form"> <input name="fave"> <input name="name"/> <button>Submit Vote</button> </form>下面,我们可以为该表单指定不同的编码方式,不同的编码方式导致不同的数据组织方式,如下:
fave:type name:name
------WebKitFormBoundary1A6ksXdtYBD9IqPJ Content-Disposition: form-data; name="fave" type ------WebKitFormBoundary1A6ksXdtYBD9IqPJ Content-Disposition: form-data; name="name" name ------WebKitFormBoundary1A6ksXdtYBD9IqPJ--
<form autocomplete="off" method="post" action="http://titan:8080/form"> <input name="fave"/> <input name="name"/> <button>Submit Vote</button> </form>autocomplete属性也可用于input元素,控制单个元素的自动完成功能,例如:
<form autocomplete="off" method="post" action="http://titan:8080/form"> <input autocomplete="on" name="fave"/> <input name="name"/> <button>Submit Vote</button> </form>
<form target="_blank" method="post" action="http://titan:8080/form"> <input autocomplete="on" name="fave"/> <input name="name"/> <button>Submit Vote</button> </form>
<form name="fruitvote" id="fruitvote" method="post" action="http://titan:8080/form"> <input autocomplete="on" name="fave"/> <input name="name"/> <button>Submit Vote</button> </form>
<form method="post" action="http://titan:8080/form"> <p><label for="fave">Fruit: <input id="fave" name="fave"/></label></p> <p><label for="name">Name: <input id="name" name="name"/></label></p> <button>Submit Vote</button> </form>
<form method="post" action="http://titan:8080/form"> <p><label for="fave">Fruit: <input autofocus id="fave" name="fave"/></label></p> <p><label for="name">Name: <input id="name" name="name"/></label></p> <button>Submit Vote</button> </form>浏览器显示页面时就会将焦点放到fave输入框上,如果一个页面有几个元素都设置了该属性,浏览器会自动聚焦于其中的最后一个元素。
<form method="post" action="http://titan:8080/form"> <p><label for="fave">Fruit: <input autofocus id="fave" name="fave"/></label></p> <p><label for="name">Name: <input disabled id="name" name="name"/></label></p> <button>Submit Vote</button> </form>
<form method="post" action="http://titan:8080/form"> <fieldset> <p><label for="name">Name: <input id="name" name="name"/></label></p> <p><label for="city">City: <input id="city" name="city"/></label></p> </fieldset> <fieldset> <p><label for="fave1">#1: <input id="fave1" name="fave1"/></label></p> <p><label for="fave2">#2: <input id="fave2" name="fave2"/></label></p> <p><label for="fave3">#3: <input id="fave3" name="fave3"/></label></p> </fieldset> <button>Submit Vote</button> </form>
<form method="post" action="http://titan:8080/form"> <fieldset> <legend>Enter Your Details</legend> <p><label for="name">Name: <input id="name" name="name"/></label></p> <p><label for="city">City: <input id="city" name="city"/></label></p> </fieldset> <fieldset> <legend>Vote For Your Three Favorite Fruits</legend> <p><label for="fave1">#1: <input id="fave1" name="fave1"/></label></p> <p><label for="fave2">#2: <input id="fave2" name="fave2"/></label></p> <p><label for="fave3">#3: <input id="fave3" name="fave3"/></label></p> </fieldset> <button>Submit Vote</button> </form>
<form method="post" action="http://titan:8080/form"> <fieldset> <legend>Enter Your Details</legend> <p><label for="name">Name: <input id="name" name="name"/></label></p> <p><label for="city">City: <input id="city" name="city"/></label></p> </fieldset> <fieldset disabled> <legend>Vote For Your Three Favorite Fruits</legend> <p><label for="fave1">#1: <input id="fave1" name="fave1"/></label></p> <p><label for="fave2">#2: <input id="fave2" name="fave2"/></label></p> <p><label for="fave3">#3: <input id="fave3" name="fave3"/></label></p> </fieldset> <button>Submit Vote</button> </form>
<form> <p> <label for="fave">Fruit:<input autofocus id="fave" name="fave"/></label> </p> <p> <label for="name">Name:<input id="name" name="name"/></label> </p> <button type="submit" formaction="http://titan:8080/form" formmethod="post">Submit Vote</button> </form>
<form method="post" action="http://titan:8080/form"> <p><label for="fave">Fruit: <input autofocus id="fave" name="fave"/></label></p> <p><label for="name">Name: <input disabled id="name" name="name"/></label></p> <button type="submit">Submit Vote</button> <button type="reset">Reset</button> </form>
<form method="post" action="http://titan:8080/form"> <p><label for="fave">Fruit: <input autofocus id="fave" name="fave"/></label></p> <p><label for="name">Name: <input disabled id="name" name="name"/></label></p> <button type="submit">Submit Vote</button> <button type="reset">Reset</button> <button type="button">Do <strong>NOT</strong> press this button</button> </form>你可以写一些JavaScript脚本,在按下按钮使执行。
<form method="post" action="http://titan:8080/form"> <p><label for="fave">Fruit: <input autofocus id="fave" name="fave"/></label></p> </form> <p><label for="name">Name: <input form="voteform" id="name" name="name"/></label></p> <button form="voteform" type="submit">Submit Vote</button>form属性中的值为对应form的id,可以指定多个id,使用空格分隔。
原文:http://blog.csdn.net/tomato__/article/details/50637764