
批改狀態(tài):合格
老師批語:
HTML <form>
元素表示文檔中的一個區(qū)域,此區(qū)域包含交互控件,用于收集用戶的信息和數(shù)據(jù),向 Web 服務器提交信息。
常見的表單域如用戶登錄界面:
表單域由<form>
標簽創(chuàng)建,格式如下:
<form name="表單名稱" method="發(fā)送數(shù)據(jù)的方法(get/post)" action="后端處理頁面">
表單元素(標簽、輸入框、按鈕等)
</form>
其中:
method 屬性:發(fā)送表單數(shù)據(jù)的方法,常用的有 get、post:
action 屬性:指定處理表單數(shù)據(jù)的后端頁面(向何處發(fā)送表單數(shù)據(jù))
<input>
表單輸入元素HTML<input>
元素用于為基于 Web 的表單創(chuàng)建交互式控件,以便接受來自用戶的數(shù)據(jù)。<input>
元素是目前是 HTML 中最強大、最復雜的元素之一,因為它有大量的輸入類型和屬性組合。
其中:
<input type="類型" name="名稱" 其他可選屬性 >
<input>
的類型,如文本框、密碼框、單選框、提交按鈕等,默認為 “text”。頁面顯示效果:
<form>
<input type="text" name="userName" value="" placeholder="請輸入內(nèi)容">
</form>
頁面顯示效果:
<form>
<input type="password" name="userpassword" value="123456">
</form>
頁面顯示效果:
<form>
<input type="submit" name="submit1" value="提交">
</form>
頁面顯示效果:
<form>
<input type="reset" value="重置">
</form>
頁面顯示效果:
<form>
男孩<input type="radio" name="gender" value="boy" checked>
女孩<input type="radio" name="gender" value="girl">
</form>
頁面顯示效果:
<form>
愛好:
<input type="checkbox" name="music" value="1" checked>音樂
<input type="checkbox" name="sport" value="2">體育
<input type="checkbox" name="reading" value="3">閱讀
</form>
<label>
標簽HTML <label>
元素(標簽)表示用戶界面中某個元素的說明。通常與<input>
輸入元素一起使用,為<input>
定義標注。
當點擊標簽文字是,關聯(lián)的<input>
元素會被激活獲取焦點,就像直接點擊<input>
元素一樣。這擴大了元素的可點擊區(qū)域,讓包括使用觸屏設備在內(nèi)的用戶更容易激活這個元素。
將一個 <label>
和一個 <input>
元素匹配在一起方法有兩種:
<input>
一個 id
屬性。而 <label>
需要一個 for
屬性,其值和 <input>
的 id
一樣:頁面顯示效果:
<form>
<label for="inputText1">用戶名:</label>
<input type="text" name="userName" id="inputText1" value="" placeholder="請輸入用戶名">
</form>
<input>
直接放在 <label>
里,此時則不需要 for
和 id
屬性,因為關聯(lián)已隱含存在:頁面顯示效果:
<form>
<label>
用戶名:
<input type="text" name="userName" value="" placeholder="請輸入用戶名">
</label>
</form>
<select>
下拉列表HTML <select>
元素表示一個提供選項菜單的控件,可創(chuàng)建單選或多選菜單,<select>
元素中的<option>
標簽用于定義列表中的可用選項。
頁面顯示效果:
<form>
<label>
城市:
<select name="city" id="city">
<option value="shanghai">上海</option>
<option value="beijing">北京</option>
<option value="suzhou" selected>蘇州</option>
</select>
</label>
</form>
ctrl
鍵選取多個選項)。頁面顯示效果:
<form>
<label>
城市:
<select name="city" id="city" multiple>
<option value="shanghai">上海</option>
<option value="beijing">北京</option>
<option value="suzhou" selected>蘇州</option>
</select>
</label>
</form>
<button>
按鈕HTML<button>
元素表示一個可點擊的按鈕,可以用在表單或文檔其他需要使用簡單標準按鈕的地方。示例:
代碼:
<form>
<button type="submit">提交</button>
</form>
頁面顯示效果:
<button>
元素比 <input>
元素更容易使用樣式??梢栽?code><button> 元素內(nèi)添加 HTML 內(nèi)容(像 <em>
、<strong>
甚至 <img>
),而 <input>
只支持文本內(nèi)容。
注意:在<form>
表單域中使用<button>
時,需要始終指定 type 值( button / submit ),因為在不同瀏覽器中,<button>的 type 默認值不一樣。
<textarea>
文本域HTML<textarea>
元素表示一個多行純文本編輯控件,當希望用戶輸入一段相當長的、不限格式的文本,例如評論或反饋表單中的一段意見時,這很有用。
代碼:
<form>
<textarea name="textarea" rows="10" cols="50">Write something here</textarea>
</form>
頁面顯示效果:
可以通過 cols 和 rows 屬性來規(guī)定<textarea>
的尺寸,不過更好的辦法是使用 CSS 的 height 和 width 屬性。
<fieldset>
分組HTML <fieldset> 元素用于對表單中的控制元素進行分組。用內(nèi)置的<legend>
元素作為 <fieldset> 的標題。
代碼:
<form action="#">
<fieldset>
<legend>分組標題</legend>
<input type="radio" id="radio">
<label for="radio">單選框</label>
</fieldset>
</form>
頁面顯示效果:
以下是應用上述知識點做的一個用戶注冊界面實例。
代碼:
<h3>新用戶注冊</h3>
<form name="userRegister" method="post" action="#">
<fieldset>
<legend>基本信息</legend>
<p>
<label for="userName">用戶名:</label>
<input id="userName" type="text" required>
</p>
<p>
<label for="userPassword">密碼:</label>
<input id="userPassword" type="password" placeholder="6~8位字母和數(shù)字" required>
</p>
</fieldset>
<fieldset>
<legend>詳細信息</legend>
<p>
<label for="email">郵箱:</label>
<input id="email" type="email">
</p>
<p>
性別:
<label for="male">男:</label>
<input name="sex" id="male" value="male" type="radio" checked>
<label for="female">女:</label>
<input name="sex" id="female" value="female" type="radio">
</p>
<p>
<label for="education">學歷:</label>
<select name="edu" id="education">
<option value="1">初中</option>
<option value="2">高中</option>
<option value="3" selected>本科</option>
<option value="4">碩士</option>
<option value="5">博士</option>
</select>
</p>
<p>
愛好:
<input type="checkbox" name="program" value="1" checked>編程
<input type="checkbox" name="sport" value="2">體育
<input type="checkbox" name="reading" value="3">閱讀
<input type="checkbox" name="trip" value="4" checked>旅游
<input type="checkbox" name="other" value="5">其他
</p>
自我介紹:
<p>
<textarea name="textarea" rows="10" cols="50">
</textarea>
</p>
</fieldset>
<button type="submit">提交</button>
<button type="reset">重置</button>
</form>
頁面顯示效果:
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號