DIV CSS 佈局教程網

您现在的位置: DIV+CSS佈局教程網 >> 網頁腳本 >  >> HTML基礎知識 >> HTML入門知識
9.2 HTML <form> 標籤
編輯:HTML入門知識     

一、表單標籤 <form>

建立表單的邏輯跟建立表格很類似:表格要把行列都放進 <table> 裡,表單則要把各種表單元件都放進 <form> 裡。form 就是表單的外殼。

語法:

<form>表單各種標籤</form>

舉例:

<!DOCTYPE html>
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<title>表單 form 標籤</title>
</head>
<body>
<form>
<input type="text" value="這是一個文字框"/><br/>
<textarea></textarea><br/>
<select>
<option>HTML</option>
<option>CSS</option>
<option>JavaScript</option>
</select>
</form>
</body>
</html>

二、form 標籤的屬性

form 有 action、method、enctype、target 四個屬性,另外常搭配 name 使用。這些屬性偏抽象、實操性不強,初學時有點摸不著頭緒很正常,先有個概念就好,等學了動態網頁之後會豁然開朗。

1、表單名稱 name

一個頁面可能有多個表單,為了區分它們,也為了避免資料送到後端後搞不清楚是哪一份,我們用 name 幫表單命名。

<form name="表單名稱">
……
</form>

表單名稱不能包含特殊字元與空格。

<!DOCTYPE html>
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<title>表單 form 標籤</title>
</head>
<body>
<form name="form1">
</form>
</body>
</html>

name="form1" 表示這個表單叫 form1。

2、提交表單 action

action 指定表單資料要送到哪個位址去處理。

<form action="表單的處理程式">
……
</form>

處理程式的位址可以是相對路徑、絕對路徑,也可以是其他形式的位址。

<!DOCTYPE html>
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<title>表單 form 標籤</title>
</head>
<body>
<p>如果您對 學習網有任何意見和建議,請發郵件給我們</p>
<form name="form1" action="mailto:lvyestudy@foxmail.com"></form>
</body>
</html>

"mailto:lvyestudy@foxmail.com" 代表以郵件形式提交。

3、傳送方法 method

method 告訴瀏覽器要用哪一種 HTTP 方法送出表單,取值為 get 或 post。

form 標籤的 method 屬性
method 屬性值說明
get預設值。表單資料被附加到 action 指定的 URL 之後,再交給處理程式
post表單資料被放進主體內容中,再傳送給處理程式

兩者的關鍵差異在安全性:get 的所有欄位值都直接暴露在網址上,post 則能把資料藏在主體裡。所以實際開發幾乎一律選 post。

<form method="傳送方法">
……
</form>
<!DOCTYPE html>
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<title>表單 form 標籤</title>
</head>
<body>
<p>如果您對 學習網有任何意見和建議,請發郵件給我們</p>
<form name="form1" action="mailto:lvyestudy@foxmail.com" method="post"></form>
</body>
</html>

4、目標顯示方式 target

form 的 target 與 <a> 標籤的 target 用法相同,用來決定結果在哪個視窗開啟,細節可參考「超鏈接 a 標籤」。

<form> 標籤 target 屬性
target 屬性值說明
_self預設值,在目前視窗開啟
_blank在一個全新的視窗開啟
_parent在父級視窗中開啟
_top載入到最頂層視窗,取代該視窗中的所有頁面

實務上只用得到 _self 與 _blank,另外兩個幾乎用不到。另外注意這四個值都以底線「_」開頭,很容易漏寫。

<form target="目標顯示方式">
……
</form>
<!DOCTYPE html>
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<title>表單 form 標籤</title>
</head>
<body>
<p>如果您對 學習網有任何意見和建議,請發郵件給我們</p>
<form name="form1" action="mailto:lvyestudy@foxmail.com" method="post" target="_blank"></form>
</body>
</html>

5、編碼方式 enctype

enctype 用來設定表單資料的提交編碼方式。

<form> 標籤的 enctype 屬性
enctype 屬性值說明
application/x-www-form-urlencoded預設的編碼方式
multipart/form-dataMIME 編碼,凡是「上傳檔案」的表單都必須選這個值

一般情況用預設值就好,也就是不必特別設定 enctype。

<!DOCTYPE html>
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<title>表單 form 標籤</title>
</head>
<body>
<p>如果您對 學習網有任何意見和建議,請發郵件給我們</p>
<form name="form1" action="mailto:lvyestudy@foxmail.com" method="post" target="_blank" enctype="application/x-www-form-urlencoded"></form>
</body>
</html>

三、表單物件

所謂表單物件,就是放在 <form> 內部的那些標籤,最常見的文字框、下拉列表都算。它們分三類:① input;② textarea;③ select 與 option。

接下來我們逐一介紹。但請牢牢記住:表單標籤只有四個——<input>、<textarea>、<select>、<option>,其中 select 與 option 必須搭配使用,就像 ol 與 li。把這點想通,思路就清楚了。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved