HTML表單是一個(gè)包含各種表單元素的區(qū)域,用于收集用戶提交的各種類型的的數(shù)據(jù)。
表單使用<from>來設(shè)置
<from>的常用屬性
action :表單提交的地址
method :提交表單的方法,有g(shù)et和post兩種。
常用的表單元素
常用表單元素屬性:
type: 輸入類型
name: 表單名稱
value: 表單的默認(rèn)值
文本域(Text Fields)
文本域通過<input type="text"> 標(biāo)簽來設(shè)定,當(dāng)用戶要在表單中鍵入用戶名、郵箱等少量?jī)?nèi)容時(shí),就會(huì)用到文本域。
<label>標(biāo)簽用于對(duì)<input>的注釋,通常配合for屬性使用。與id屬性相對(duì)應(yīng)。
例如
<form action="/getInfo" method="get">
<label for="username">姓名</label>
<input id="username" type="text" name="username" value="ruo">
</form>
密碼字段
通過標(biāo)簽**<input type="password"> **來定義:
<form action="/getInfo" method="get">
<label for="password">密碼</label>
<input id="password" type="password" name="password" placeholder="輸入密碼">
</form>
單選框(Radio buttons)
通過標(biāo)簽<input type="radio"> 定義。
<form action="/getInfo" method="get">
<input type="radio" name="sex" value="男"> 男
<input type="radio" name="sex" value="女"> 女
</form>
復(fù)選框(Checkboxes)
通過標(biāo)簽**<input type="checkbox"> **定義。
例如
<form action="/getInfo" method="get">
<input type="checkbox" name="hobby" value="read"> 讀書
<input type="checkbox" name="hobby" value="music"> 聽歌
<input type="checkbox" name="hobby" value="study"> 學(xué)習(xí)
</form>
提交按鈕(Submit Button)
通過<input type="submit"> 定義
當(dāng)用戶點(diǎn)擊提交按鈕時(shí),表單的內(nèi)容會(huì)被傳送。傳送的方式和目的地由<form>標(biāo)簽定義。
例如
<form action="/getInfo" method="get">
<input type="submit" value="Submit" /> 提交
</form>
重置按鈕(Reset)
通過<input type="reset">定義
點(diǎn)擊按鈕時(shí),清空填寫的表單數(shù)據(jù)。
<form action="/getInfo" method="get">
<input type=reset"">
<form action="/getInfo" method="get">