HTML标签之常见表单标签

<form>标签

<form>用于创建供用户输入的HTML表单。
属性:
action=""  规定表单提交时向何处发送数据。

method="post/get"  规定发送表单数据的HTTP方法。

name=""  表单名。

target=""  规定打开action的url的方式。
_blank 新窗口打开

_self  同一框架中打开(默认)

_top  整个窗口中打开

_parent 父窗口打开

<form action="xxx.php" name="form1" method="post/get" target="_blank">
  
</form>


<input>标签


<input>标签声明允许用户输入数据的input控件,输入数据的方式有很多,取决于type属性。

type:  number、time、year、month、data、url、email、image、range、file、search、submit、button、text、tel(电话号码字段)、radio、checkbox。

input标签属性介绍:

accept属性

规定上传文件服务器接收的文件类型,仅适用于type="file",accept属性值有:audio/*、video/*、image/*、MIME_type。

例:当accept="image/*"时,type="file",控件为上传文件类型。


<form action="xxx.php">
  上传的文件:<input type="file" name="input1" accept="image/*"><br>
  <input type="button" name="but1" value="提交">
</form>


alt属性

  alt属性规定,当type="image"时,用户由于某些原因(如网络差、图片路径不正确等)无法查看图像时,可以用alt提供替代文本。

<form action="demo_form.php">
  First name: <input type="text" name="fname"><br>
  <input type="image" src="submit.gif" alt="Submit" width="48" height="48">
</form>

<p>点击图片,输入框将被发送到服务器的“demo_form.php”页面。</p>
<p><b>注意:</b> 如果 type 属性设置为 image,当用户单击图像时,浏览器将以像素为单位,将鼠标相对于图像边界的偏移量发送到服务器,其中包括从图像左边界开始的水平偏移量,以及从图像上边界开始的垂直偏移量。</p>


autocomplete属性

autocomplete属性规定当在表单中输入数据时,浏览器是否自动完成数据的输入,autocomplete="on|off"。

<form action="xxx.php" name="from1" method="post" autocomplete="on">
  First name:<input type="text" name="input2" autocomplete="on"><br>
  Last name: <input type="text" name="input3" autocoplete="on"><br>
 Email:<input type="email" name="email1"autocomplete="off"><br>
<input type="submit" name="sub1" value="提交">
</from>


autofocus属性

autofocus属性规定当页面加载时,<input>元素是否自动获取焦点(聚焦)。autofocus="autofocus"或autofocus

<form action="demo-form.php">
  First name: <input type="text" name="fname" autofocus><br>
  Last name: <input type="text" name="lname"><br>
  <input type="submit">
</form>


checked属性

checked属性规定当<input>元素的type="checkbox"或type="radio"时,是否预先选择

水果<input type="checkbox" name="input4" checked><br>
蔬菜<input type="checkbox" name="input5"><br>
足球<input type="radio" name="input6" checked>
篮球<input type="radio" name="input6">



disabled属性


disabled发展规定是否禁用<input>元素。当<input>元素禁用时,用户不能进行数据输入。

帐号:<input type="text" name="input7" ><br>
密码:<input type="password" name="possword"disabled >



placeholder属性


placeholder属性规定<input>元素提示用户输入数据。

姓名:<input type="text" name="input8" placeholder="请输入姓名"><br>
          <input type="submit" name="sub3" value="提交">



min、max属性

规定<input>元素输入数据的最小、最大值。

maxlength属性

规定<input>元素中输入数据的最大字符长度。

<input type="text" name="usrname" maxlength="10">


以上输入的数据是长为10个字符,即使再输入也没有反应。


step属性

step规定当<input>元素的type="number"时的有效数字间隔。
<input type="number" step="3" min="0" max="20">

间隔为3,点击一次,数字累积加3。


src、width、height属性


src属性规定了当<input>元素的type="image"时图像的源。width、height属性规定了当<input>元素的type=“image”时图像的宽高。

 First name: <input type="text" name="fname"><br>
  <input type="image" src="submit.gif" alt="Submit" width="48" height="48">



readonly属性


readonly属性规定了<input>元素中的数据为只读模式,不能修改和输入。

姓名:<input type="text" name="inp1"><br>
号码:<input type="text" name="inp2" value="1111" readonly>

<textarea>标签

<textarea>标签定义了多行文本输入控件。


cols、rows分别规定了文本控件的可见列数和行数。

<input>元素的一些属性也适用于<textarea>元素

wrap="hard|soft"规定了多行文本输入中的内容是否可以换行。hard为可以换行,soft为默认(不可换行)。当确定能换行(即wrap="hard")时,必需指定rows值。

<textarea name="text1" cols="20" rows="10" wrap="hrad" autofocus>
默认内容
</textarea>

<select>、<option>标签

<select>标签定义了一个下拉列表。

<option>标签定义下拉列表的选项。

required属性规定了下拉列表中必须选择一项。(目前没有主浏览器支持该属性

size属性规定下拉列表可见选项的数目。


<form action="demo_form.php">
<select required size="3">
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="mercedes">Mercedes</option>
  <option value="audi">Audi</option>
</select>
<input type="submit">
</form>




optgroup属性规定了下拉列表中的组合。

label属性为选项组规定描述标签。


<select>
  <optgroup label="Swedish Cars"><!--label为选项组规定描述标签-->
    <option value="volvo">Volvo</option>
    <option value="saab">Saab</option>
  </optgroup>
  <optgroup label="German Cars">
    <option value="mercedes">Mercedes</option>
    <option value="audi">Audi</option>
  </optgroup>
</select>

组合更能看清选项属于那一类:


<fieldset>标签

<fieldset>标签为列表绘制边框。legend属性定义边框的标题。

<form action="xxx.php">
  <fieldset name="field1">
    <legend>边框标题</legend>
     <input type="text"><br>
     <input type="text"><br>
   </fieldset>
</form>


当没有列表,也可以绘制一个边框,里面没有内容,只有一个标题。


<form>
 <fieldset>
  <legend>边框标题</legend>  
 </fieldset>
</form>



<datalist>标签

<datalist>标签规定了<input>元素可能的选项。由下拉框表示。

<form action="xxx.php">
  <input list="fruit" name="fru">
  <datalist id="fruit">
    <option value="apple">apple</option>
    <option value="apple1">apple1</option>
    <option value="apple2">apple2</option>
  </datalist>
</form>


<input>list属性值与 <datalist>的id相对应。



<label>标签

<label>为<input>元素定义标注(标记)。

<label for="xx">文本</label>
<input id="xx">

for属性值与id对应。


<p>点击其中一个文本标签选中选项:</p>

<form action="demo_form.phpp">
  <label for="male">Male</label>
  <input type="radio" name="sex" id="male" value="male"><br>
  <label for="female">Female</label>
  <input type="radio" name="sex" id="female" value="female"><br><br>
  <input type="submit" value="提交">
</form>


<label>标签的for属性值需与input元素的id相对应,这样点击文本标签才能选中选项。




  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值