HTML5中存在多种不同的输入框和按钮,通过设置input元素的type属性来实现,除此之外,HTML5中还支持选择列表、多行输入框等,这些元素都有自己的用途和属性,下面一一介绍。
<form method="post" action="http://titan:8080/form"> <p><label for="name">Name: <input maxlenth="10" id="name" name="name"/></label></p> <p><label for="city">City: <input size="10" id="city" name="city"/></label></p> <p><label for="fave">Fruit: <input size="10" maxlenth="10" id="fave" name="fave"/></label></p> <button type="submit">Submit Vote</button> </form>
<form method="post" action="http://titan:8080/form"> <p><label for="name">Name: <input placeholder="Your name" id="name" name="name"/></label></p> <p><label for="city">City: <input placeholder="Where you live" id="city" name="city"/></label></p> <p><label for="fave">Fruit: <input value="Apple" id="fave" name="fave"/></label></p> <button type="submit">Submit Vote</button> </form>在chrome中的效果如下:
<form method="post" action="http://titan:8080/form"> <p><label for="name">Name: <input placeholder="Your name" id="name" name="name"/></label></p> <p><label for="city">City: <input placeholder="Where you live" id="city" name="city"/></label></p> <p><label for="fave">Fruit: <input list="fruitlist" id="fave" name="fave"/></label></p> <button type="submit">Submit Vote</button> </form> <datalist id="fruitlist"> <option value="Apples" label="Lovely Apples"/> <option value="Oranges">Refreshing Oranges</option> <option value="Cherries"/> </datalist>datalist元素中的每一个option都代表一个用户可选择的值,在chrome中的效果如下:
<form method="post" action="http://titan:8080/form"> <p><label for="name">Name: <input value="Adam" disabled id="name" name="name"/></label></p> <p><label for="city">City: <input value="Boston" readonly id="city" name="city"/></label></p> <p><label for="fave">Fruit: <input value="Apple" id="fave" name="fave"/></label></p> <button type="submit">Submit Vote</button> </form>在chrome中效果如下:
<form method="post" action="http://titan:8080/form"> <p><label for="name">Name: <input value="Adam" disabled id="name" name="name"/></label></p> <p><label for="password">Password: <input type="password" placeholder="Min 6 characters" id="password" name="password"/></label></p> <p><label for="fave">Fruit: <input value="Apples" id="fave" name="fave"/></label></p> <button type="submit">Submit Vote</button> </form>用户输入的字符在密码框中显示为"*",但需要注意,在提交表单时,服务器收到的是明文密码,对于安全至关重要的网站和应用系统,应该考虑使用SSL/HTTPS对浏览器和服务器之间的通信内容加密。
<form method="post" action="http://titan:8080/form"> <p><label for="name">Name: <input value="Adam" disabled id="name" name="name"/></label></p> <p><label for="password">Password: <input type="password" placeholder="Min 6 characters" id="password" name="password"/></label></p> <p><label for="fave">Fruit: <input value="Apples" id="fave" name="fave"/></label></p> <input type="submit" value="Submit Vote"/> <input type="reset" value="Reset"/> <input type="button" value="My Button"/> </form>在chrome中的效果如下:
<form method="post" action="http://titan:8080/form"> <p><label for="name">Name: <input value="Adam" disabled id="name" name="name"/></label></p> <p><label for="password">Password: <input type="password" placeholder="Min 6 characters" id="password" name="password"/></label></p> <p><label for="fave">Fruit: <input value="Apples" id="fave" name="fave"/></label></p> <p><label for="price">$ per unit in your area: <input type="number" step="1" min="0" max="100" value="1" id="price" name="price"/></label></p> <input type="submit" value="Submit Vote"/> </form>在chrome中的效果如下:
<form method="post" action="http://titan:8080/form"> <p><label for="name">Name: <input value="Adam" disabled id="name" name="name"/></label></p> <p><label for="password">Password: <input type="password" placeholder="Min 6 characters" id="password" name="password"/></label></p> <p><label for="fave">Fruit: <input value="Apples" id="fave" name="fave"/></label></p> <p><label for="price">$ per unit in your area: 1<input type="range" step="1" min="0" max="100" value="1" id="price" name="price"/>100</label></p> <input type="submit" value="Submit Vote"/> </form>在chrome中的效果如下:
<form method="post" action="http://titan:8080/form"> <p><label for="name">Name: <input value="Adam" disabled id="name" name="name"/></label></p> <p><label for="password">Password: <input type="password" placeholder="Min 6 characters" id="password" name="password"/></label></p> <p><label for="fave">Fruit: <input value="Apples" id="fave" name="fave"/></label></p> <p><label for="veggie">Are you vegetarian: <input type="checkbox" id="veggie" name="veggie"/>vegetarian</label></p> <input type="submit" value="Submit Vote"/> </form>在chrome中的效果如下:
<form method="post" action="http://titan:8080/form"> <p><label for="name">Name: <input value="Adam" disabled id="name" name="name"/></label></p> <p><label for="password">Password: <input type="password" placeholder="Min 6 characters" id="password" name="password"/></label></p> <p><fieldset> <legend>Vote for your favorite fruit</legend> <label for="apples"> <input type="radio" checked value="Apples" id="apples" name="fave"/> Apples </label> <label for="oranges"> <input type="radio" value="Oranges" id="oranges" name="fave"/> Oranges </label> <label for="cherries"> <input type="radio" value="Cherries" id="cherries" name="fave"/> Cherries </label> </fieldset></p> <input type="submit" value="Submit Vote"/> </form>在chrome中的效果如下:
<form method="post" action="http://titan:8080/form"> <p><label for="name">Name: <input value="Adam" disabled id="name" name="name"/></label></p> <p><label for="password">Password: <input type="password" placeholder="Min 6 characters" id="password" name="password"/></label></p> <p><label for="email">Email: <input type="email" placeholder="user@domain.com" id="email" name="email"/></label></p> <p><label for="tel">Tel: <input type="tel" placeholder="(xxx)-xxx-xxxx" id="tel" name="tel"/></label></p> <p><label for="url">Your homepage: <input type="url" id="url" name="url"/></label></p> <input type="submit" value="Submit Vote"/> </form>在chrome中的效果如下:
<form method="post" action="http://titan:8080/form"> <p><label for="name">Name: <input value="Adam" disabled id="name" name="name"/></label></p> <p><label for="password">Password: <input type="password" placeholder="Min 6 characters" id="password" name="password"/></label></p> <p><label for="fave">Fruit: <input value="Apples" id="fave" name="fave"/></label></p> <p><label for="lastbuy">When did you last buy: <input type="date" id="lastbuy" name="lastbuy"/></label></p> <input type="submit" value="Submit Vote"/> </form>在chrome中的效果如下:
<form method="post" action="http://titan:8080/form"> <p><label for="name">Name: <input value="Adam" disabled id="name" name="name"/></label></p> <p><label for="password">Password: <input type="password" placeholder="Min 6 characters" id="password" name="password"/></label></p> <p><label for="fave">Fruit: <input value="Apples" id="fave" name="fave"/></label></p> <p><label for="color">Color: <input type="color" id="color" name="color"/></label></p> <input type="submit" value="Submit Vote"/> </form>在chrome中的效果如下:
<form method="post" action="http://titan:8080/form"> <input type="hidden" name="recordID" value="1234"/> <p><label for="name">Name: <input value="Adam" disabled id="name" name="name"/></label></p> <p><label for="password">Password: <input type="password" placeholder="Min 6 characters" id="password" name="password"/></label></p> <p><label for="fave">Fruit: <input value="Apples" id="fave" name="fave"/></label></p> <input type="submit" value="Submit Vote"/> </form>
<form method="post" action="http://titan:8080/form"> <input type="hidden" name="recordID" value="1234"/> <p><label for="name">Name: <input value="Adam" disabled id="name" name="name"/></label></p> <p><label for="password">Password: <input type="password" placeholder="Min 6 characters" id="password" name="password"/></label></p> <p><label for="fave">Fruit: <input value="Apples" id="fave" name="fave"/></label></p> <input type="image" src="accept.png" name="submit"/> </form>点击图像按钮将提交表单,在提交的数据中会包含点击位置的坐标信息,因此可以让图像中的不同区域代表不同的操作,然后根据用户在图像上的点击位置做出相应的反应。
<form method="post" action="http://titan:8080/form" enctype="multipart/form-data"> <p><label for="name">Name: <input value="Adam" disabled id="name" name="name"/></label></p> <p><label for="password">Password: <input type="password" placeholder="Min 6 characters" id="password" name="password"/></label></p> <p><label for="fave">Fruit: <input value="Apples" id="fave" name="fave"/></label></p> <p><input type="file" name="filedata"/></p> <input type="submit" value="Submit Vote"/> </form>注意表单编码类型为multipart/form-data的时候才能上传文件。在chrome中的效果如下:
<form method="post" action="http://titan:8080/form" enctype="multipart/form-data"> <p><label for="name">Name: <input value="Adam" disabled id="name" name="name"/></label></p> <p><label for="password">Password: <input type="password" placeholder="Min 6 characters" id="password" name="password"/></label></p> <p><label for="fave"> Favorite Fruit: <select id="fave" name="fave"> <option valu="apples" selected label="Apples">Apples</option> <option valu="oranges" label="Oranges">Oranges</option> <option valu="cherries" label="Cherries">Cherries</option> <option valu="pears" label="Pears">Pears</option> </select> </label></p> <input type="submit" value="Submit Vote"/> </form>在chrome中的效果如下:
<form method="post" action="http://titan:8080/form" enctype="multipart/form-data"> <p><label for="name">Name: <input value="Adam" disabled id="name" name="name"/></label></p> <p><label for="password">Password: <input type="password" placeholder="Min 6 characters" id="password" name="password"/></label></p> <p><label for="fave"> Favorite Fruit: <select id="fave" name="fave" size="5" multiple> <option valu="apples" selected label="Apples">Apples</option> <option valu="oranges" label="Oranges">Oranges</option> <option valu="cherries" label="Cherries">Cherries</option> <option valu="pears" label="Pears">Pears</option> </select> </label></p> <input type="submit" value="Submit Vote"/> </form>在点击选项时按住Ctrl键,就可以选择多个选项。在chrome中效果如下:
<form method="post" action="http://titan:8080/form" enctype="multipart/form-data"> <p><label for="name">Name: <input value="Adam" disabled id="name" name="name"/></label></p> <p><label for="password">Password: <input type="password" placeholder="Min 6 characters" id="password" name="password"/></label></p> <p><label for="fave"> Favorite Fruit: <select id="fave" name="fave"> <optgroup label="Top Choices"> <option valu="apples" selected label="Apples">Apples</option> <option valu="oranges" label="Oranges">Oranges</option> </optgroup> <optgroup label="Others"> <option valu="cherries" label="Cherries">Cherries</option> <option valu="pears" label="Pears">Pears</option> </optgroup> </select> </label></p> <input type="submit" value="Submit Vote"/> </form>在Chrome中的效果如下:
<form method="post" action="http://titan:8080/form" enctype="multipart/form-data"> <p><label for="name">Name: <input value="Adam" disabled id="name" name="name"/></label></p> <p><label for="password">Password: <input type="password" placeholder="Min 6 characters" id="password" name="password"/></label></p> <p><label for="fave"> Favorite Fruit: <select id="fave" name="fave"> <optgroup label="Top Choices"> <option valu="apples" selected label="Apples">Apples</option> <option valu="oranges" label="Oranges">Oranges</option> </optgroup> <optgroup label="Others"> <option valu="cherries" label="Cherries">Cherries</option> <option valu="pears" label="Pears">Pears</option> </optgroup> </select> </label></p> <p><textarea cols="20" rows="5" wrap="hard" id="story" name="story">Tell us why this is your favorite fruit</textarea></p> <input type="submit" value="Submit Vote"/> </form>在Chrome中的效果如下:
<form onsubmit="return false" oninput="res.value = quant.valueAsNumber * price.valueAsNumber"> <fieldset> <legend>Price Calculator</legend> <input type="number" placeholder="Quantity" id="quant" name="quant"/> <input type="number" placeholder="Price" id="price" name="price"/> = <output for="quant name" name="res"/> </fieldset> </form>上面使用了JavaScript的时间系统生成了一个简单的计算器,在Chrome中的效果如下:
原文:http://blog.csdn.net/tomato__/article/details/50674539