Web 如何向服務器提交數(shù)據(jù)氏豌?答案是表單讨彼。平時我們使用的登錄框判莉、搜索框等都是表單的組成雾棺。
表單元素用<form>
標簽包裹胳螟,有四個主要屬性:
-
action
罚缕,指定表單提交的地址 -
method
迁沫,指定提交表單的方法 -
target
写妥,指定表單提交后在何處打開action
地址 -
enctype
苇倡,指定表單數(shù)據(jù)的編碼類型富纸,有三個值:- application/x-www-form-urlencoded:在發(fā)送前編碼所有字符(默認)
- text/plain:空格轉(zhuǎn)換為 "+" 加號,但不對特殊字符編碼
- multipart/form-data:使用包含文件上傳控件的表單時旨椒,必須使用該值
常見的表單元素/控件有:
-
<input type="text" />
晓褪,用于輸入可見文本 -
<input type="password" />
,用于輸入密碼综慎,瀏覽器會自動遮蔽輸入的文本 -
<input type="radio" />
涣仿,單選框 -
<input type="checkbox" />
,復選框 -
<input type="file" />
,用于上傳本地文件 -
<input type="hidden" />
好港,用于提交不需要或不應該被用戶看到的數(shù)據(jù) -
<input type="button" />
愉镰,為表單創(chuàng)建一個按鈕 -
<input type="submit" />
,為表單創(chuàng)建一個提交按鈕钧汹,默認行為是點擊時就提交表單 -
<input type="reset" />
丈探,清空表單數(shù)據(jù) -
<textarea>
,創(chuàng)建一個文本域 -
<button>
拔莱,創(chuàng)建一個按鈕碗降,默認行為是點擊時就提交表單
<label>
標簽
<label>
可以為控件添加文本標簽,點擊標簽時就可以把焦點落到對應的控件上辨宠。為控件指定標簽有兩種方法:
- 為控件添加
id
值遗锣,然后在<label>
的for
屬性中指定這個id
值货裹,如<label for='uname'>username</label><input id="uname" type="text" name="username" />
- 用
<label>
包裹控件嗤形,如<label>username<input type="text" name="username" /></label>
,無需指定id