Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537 Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537
篇介紹了表單的使用,表單有很多控件,比如輸入框,密碼框、文本域,按鈕等。按類型可分如下:
此類控件有很多種類型,使用<input type="類型">語法,常見類型如下:
type 值 | 含義 |
text | 文字字段 |
password | 密碼域,用戶看不到明文,以*代替 |
radio | 單選按鈕 |
checkbox | 多選按鈕 |
button | 普通按鈕 |
submit | 提交按鈕 |
reset | 重置按鈕 |
image | 圖像域,用圖像作為背景的提交按鈕 |
hidden | 隱藏域,不可見的輸入框 |
file | 文本域,用于上傳文件等非文本數據 |
文本輸入框和密碼框
除了顯示形式不一樣,其它屬性一樣,有以下屬性:
如下是文本輸入框和密碼框制作一個登錄表單
html代碼:
<!DOCTYPE html>
<html>
<body>
<h1>用戶登錄</h1>
<form action="/demo/html/action_page.php">
<label for="fname">用戶名:</label><br>
<input type="text" id="username" name="username" value=""><br>
<label for="lname">密碼:</label><br>
<input type="password" id="pwsd" name="pwsd" value=""><br><br>
<input type="submit" value="提交">
</form>
</body>
</html>
顯示效果:
HTML5 輸入類型
除了以上幾種類型,HTML5 還增加了多個新的輸入類型:
如下代碼:
<!DOCTYPE html>
<html>
<body>
<form action="/demo/demo_form.asp">
數字類型(1 到 5 之間):
<input type="number" name="quantity" min="1" max="5">
IE9 及早期版本不支持 type="number"。<br>
color 選擇顏色:
<input type="color" name="color"><br>
生日:
<input type="date" name="bday"><br>
年月:
<input type="month" name="bdaymonth"><br>
年周:
<input type="week" name="week_year"><br>
時間:
<input type="time" name="usr_time"><br>
一定范圍
<input type="range" name="points" min="0" max="10"><br>
E-mail:
<input type="email" name="email">
能夠在被提交時自動對電子郵件地址進行驗證<br>
搜索:
<input type="search" name="googlesearch"><br>
電話:
<input type="tel" name="usrtel">
目前只有 Safari 8 支持 tel 類型。<br>
url:
<input type="url" name="url">
提交時能夠自動驗證 url 字段<br>
<input type="submit">
</form>
</body>
</html>
效果如下:
單選和多選按鈕
使用 type = “radio” 和 type =“checkbox” 定義是單選還是多選,除了name和value屬性外,單選和多選都有一個 checked屬性定義默認選擇的項,checked = “true”指選中那個選項,表單會將 checked = “true” 的選型值傳遞給后臺。
如下實例:
<!DOCTYPE html>
<html>
<body>
<h4>單選和多選</h4>
<form action="/demo/demo_form.asp">
水果:
<input type="radio" name="shuiguo" value="banner" checked> 香蕉
<input type="radio" name="shuiguo" value="apple"> 蘋果
<br><br>
省份:
<input type="checkbox" name="shengfen" value="shannxi" checked> 陜西
<input type="checkbox" name="shengfen" value="sanxi"> 山西
<input type="checkbox" name="shengfen" value="gdong"> 廣東
<br><br>
<input type="submit">
</form>
</body>
</html>
顯示效果:
單選和多選傳遞給后臺的數據是不一樣的,如下會看到地址欄中的數據,多選會發送多個值,后臺將會獲取一個數組形式的數據。
/demo/demo_form.asp?shuiguo=banner&shengfen=shannxi&shengfen=sanxi
普通按鈕、提交按鈕、重置按鈕
普通按鈕:type = “button”,一般配合腳本使用,語法如下:
<input type="button" name="名稱" value="按鈕值" onclick="腳本程序" />
value 值就是按鈕在頁面顯示的文字,onclick屬性定義了腳本事件,這里指單擊按鈕時所進行的處理。
如下示例:
<!DOCTYPE html>
<html>
<body>
<form>
<input type="button" value="普通按鈕">
<input type="button" value="打開窗口" onclick="window.open()">
<input type="button" value="您好" onclick="alert('您好')">
</form>
</body>
</html>
單擊您好按鈕
提交按鈕:type = “submit”,用于提交表單內容,是一種特殊按鈕。
如剛才的登錄表單,提交后會返回結果:
重置按鈕:type="reset",用于清除表單數據,也是一種特殊按鈕。
輸入數據
點擊重置按鈕后,表單數據清空
重置清空數據
HTML5 按鈕
除了使用input定義按鈕,還可以使用 html5 新增的<button> 標簽定義按鈕,button 使用語法如下:
<form action="/demo/html/action_page.php">
<button type="button">普通按鈕</button>
<button type="submit">提交按鈕</button>
</form>
其它輸入類控件
隱藏域 —— hidden
文件域 —— file
如下示例:
<form action="/demo/html/action_page.php">
<label for="fname">隱藏域:</label>
<input type="hidden" id="hidden" name="hidden" value=""><br>
<label for="lname">文件域:</label>
<input type="file" id="file" name="file" value=""><br>
<input type="submit" value="提交">
</form>
顯示效果
可以看到,隱藏域在頁面中不顯示,單擊文件域選擇文件按鈕可以選擇文件,比如word文件,電子表格文件等,會以非文本方式傳送到后臺的,常用來實現文件上傳功能。
除了input 類型的控件,還有文本域 textarea ,一種特殊的文本框,它與input 文本輸入框的區別就是可以輸入多行文字,input 文本輸入框是單行的無法輸入多行文字。
如下示例:
<p>textarea 元素定義多行輸入字段。</p>
<form action="/demo/html/action_page.php">
<textarea name="message" rows="10" cols="30">The cat was playing in the garden.</textarea>
<br><br>
<input type="submit">
</form>
效果如下:
rows 屬性定義文本域的高度是幾行,cols 定義文本域寬度占幾列,比如上面定義了高10行寬30列的文本域。
下拉菜單作用和單選按鈕類似,只不過它更加節省空間,當要選擇的選型很多時,就不適合使用radio空間,所以當選項很多的時候,使用下拉菜單,語法如下:
<select name="名稱">
<option value="選項值1" selected>選項1</option>
<option value="選項值2">選項3</option>
更多option......
</select>
多選列表和多選按鈕類似,一樣為了節省空間,當數據選項比較多時,使用多選列表,語法如下:
<select name="名稱" size="可看見的列表項數" multiple>
<option value="選項值1" selected>選項1</option>
<option value="選項值2">選項3</option>
更多option......
</select>
多選比下拉菜單不同之處是多了一個multiple屬性,定義多選的,且表現形式也不一樣,不是下拉而是一個列表。
如下代碼:
<!DOCTYPE html>
<html>
<body>
<form action="/demo/demo_form.asp">
下拉菜單:<br>
<select name="cars">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="fiat">Fiat</option>
<option value="audi">Audi</option>
</select>
<br>
多選列表:<br>
<select name="cars" size="3" multiple>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="fiat">Fiat</option>
<option value="audi">Audi</option>
</select>
<br><br>
<input type="submit">
</form>
</body>
</html>
顯示效果:
這里需要注意的是,多選列表多選時需要按住ctrl鍵同時鼠標單擊選擇才能多選,效果如下:
到這里,已介紹了大部分的表單控件,現在你可以使用他們制作自己的表單,表單通常在動態網站中使用,這為以后制作動態網站打下基礎。
還有許多屬性沒有講到,比如html5新增的一些屬性和功能,可自行參考 w3cshool 等網站學習,感謝關注,學習愉快!
上篇 : 前端入門——html 表單
下篇: 前端入門 —— 網頁中使用窗口框架
單標簽
常見的語句:
form:表單標簽格式
作用:用來收集用戶輸入信息如:登入、注冊、搜索商品等
action:開始網址
method:get和post等等
text (明文):用戶名格式
password :(密文)密碼
radio :單選框 性別格式 性別是單選,單選類型是radio,注意name要加上sex
checkbox:復選框
textarea:文本框
file:上傳文件
select:下拉選擇框
button:按鈕
reset:重置
submit:提交
詳解:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>表單標簽</title>
</head>
<body>
<form action="https://hao.360.com/" method="post">
<p>賬號:<input type="text" /></p>
<p>密碼:<input type="password" /></p>
<p>
<input type="radio" name="sex" id="" />男
<input type="radio" name="sex" id="" />女
</p>
<p>
<input type="checkbox" name="" id="" value="" />linux
<input type="checkbox" name="" id="" value="" />mysql
<input type="checkbox" name="" id="" value="" />html
<input type="checkbox" name="" id="" value="" />python
</p>
<p>學歷
<select name="">
<option value="">請選擇學歷</option>
<option value="">小學</option>
<option value="">初中</option>
<option value="">高中</option>
<option value="">大學</option>
</select>
</p>
<p>自我介紹:<br />
<textarea name="" rows="10" cols="30"></textarea>
</p>
<input type="reset" value="重置"/>
<input type="submit" value="提交"/>
<input type="button" value="按鈕"/>
</form>
</body>
</html>
TML:完成頁面的內容展示
CSS:完成頁面樣式的控制,美化頁面,完成頁面的布局。
表單:用于采集用戶輸入的數據。用于和服務器進行交互。
form:用于定義表單的。可以定義一個范圍(代表用戶采集數據的范圍)
屬性:action:指定提交數據的url(指的就是把數據提交到哪里)
method:指定提交方式
分類:一共有7種,2種比較常用。
get:1.請求參數會在地址欄顯示
2.請求參數的長度是有限制的。
3.請求不安全
post:1.請求參數不會在地址欄顯示,會封裝在請求體中。
2.請求參數的長度沒有限制
3.較為安全
表單里面的數據要想被提交,必須指定它的name屬性
表單項標簽
input:可以通過type屬性值,改變元素展示的樣式。
type屬性:text:文本輸入框,默認值
placeholder:指定輸入框的提示信息,當輸入框的內容發生變化,會自動情況提示信息。
password:密碼輸入框
radio:1.單選框(要想讓多個單選框實現單選的效果,則多個單選框的name屬性值必須一樣)
2.一般會給每一個單選框提供value屬性,指定其被選中后提交的值。
3.checked屬性可以指定默認值
checkbox:復選框:
1.一般會給每一個單選框提供value屬性,指定其被選中后提交的值。
2.checked屬性可以指定默認值
file:文件選擇框
hidden:隱藏域,用于提交一些信息
按鈕:
submit:提交按鈕。可以提交表單
button:普通按鈕
image:圖片提交按鈕
src屬性指定圖片的路徑
label:指定輸入項的文字描述信息
注意:lable的for屬性一般會和input的id屬性值對應。如果對應了,點擊lable區域,會讓input輸入框獲取焦點。
select:下拉列表
子元素:option,指定列表項
textarea:文本域
*請認真填寫需求信息,我們會在24小時內與您取得聯系。