HTML+CSS基础(五):表单标签,与用户交互

来源:互联网 发布:淘宝店铺广告联盟 编辑:程序博客网 时间:2024/04/30 11:54

网站使用HTML表单(form)与用户进行交互。表单是可以把浏览者输入的数据传送到服务器端,这样服务器端程序就可以处理表单传过来的数据。

<form   method="传送方式"   action="服务器文件">

1.<form><form>标签是成对出现的,以<form>开始,以</form>结束。

2.action :浏览者输入的数据被传送到的地方,比如一个PHP页面(save.php)。

3.method : 数据传送的方式(get/post)。

<form    method="post"   action="save.php">        <label for="username">用户名:</label>        <input type="text" name="username" />        <label for="pass">密码:</label>        <input type="password" name="pass" /></form>

注意:
所有表单控件(文本框、文本域、按钮、单选框、复选框等)都必须放在标签之间(否则用户输入的信息可提交不到服务器上哦!)。


文本框

<form>   <input type="text/password" name="名称" value="文本" /></form>

1、type: 当type=”text”时,输入框为文本输入框 ; 当type=”password”时, 输入框为密码输入框。

2、name:为文本框命名,以备后台程序ASP 、PHP使用。

3、value:为文本输入框设置默认值。(一般起到提示作用)


文本域

支持多行文本输入,当用户需要在表单中输入大段文字时,需要用到文本输入域。

<textarea  rows="行数" cols="列数">文本</textarea>

1、标签是成对出现的,以开始,以结束。

2、cols :多行输入域的列数。

3、rows :多行输入域的行数。

4、在标签之间可以输入默认值。

举例:

<form  method="post" action="save.php">        <label>联系我们</label>        <textarea cols="50" rows="10" >在这里输入内容...</textarea></form>

这里写图片描述


单选框、复选框

<input   type="radio/checkbox"   value="值"    name="名称"   checked="checked"/>

1、type: 当 type=”radio” 时,控件为单选框 ; 当 type=”checkbox” 时,控件为复选框。

2、value:提交数据到服务器的值(后台程序PHP使用)

3、name:为控件命名,以备后台程序 ASP、PHP 使用

4、checked:当设置 checked=”checked” 时,该选项被默认选中

<form action="save.php" method="post" >    <label>性别:</label>    <label></label>    <input type="radio" value="1"  name="gender" />    <label></label>    <input type="radio" value="2"  name="gender" />    <br /> <br />    <lalel>你对那些运动感兴趣?</lalel><br />    <input type="checkbox" value="跑步" name="checkbox1" checked="checked"/>    <lable>跑步</lable>    <input type="checkbox" value="打球" name="checkbox2"checked="checked"/>    <lable>打球</lable>    <input type="checkbox" value="登山" name="checkbox3"/>    <lable>登山</lable>    <input type="checkbox" value="游泳" name="checkbox4"/>    <lable>游泳</lable></form>

在浏览器中显示的结果:

这里写图片描述

注意:同一组的单选按钮,name 取值一定要一致,比如上面例子为同一个名称“gender”,这样同一组的单选按钮才可以起到单选的作用。


下拉列表框

<form action="save.php" method="post" >    <label>爱好:</label>    <select>      <option value="看书">看书</option>      <option value="旅游" selected="selected">旅游</option>      <option value="运动">运动</option>      <option value="购物">购物</option>    </select></form>

在浏览器中显示的结果:
这里写图片描述


提交、重置按钮

在表单中有两种按钮可以使用,分别为:提交按钮、重置。

<input   type="submit/reset"   value="提交">

label标签

label标签不会向用户呈现任何特殊效果,它的作用是为鼠标用户改进了可用性。当用户单击选中该label标签时,就自动选中和该label标签相关连的表单控件上。

<label for="控件id名称">

注意:标签的 for 属性中的值应当与相关控件的 id 属性值一定要相同。

<form>  <label for="male"></label>  <input type="radio" name="gender" id="male" />  <br />  <label for="female"></label>  <input type="radio" name="gender" id="female" />  <label for="email">输入你的邮箱地址</label>  <input type="email" id="email" placeholder="Enter email"></form>
0 0
原创粉丝点击