單標簽
常見的語句:
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>
、表單在網頁中的應用:登錄、注冊常用到表單
2、表單的語法:
<form method="post" action="result.html">
<p> 名字:<input name="name" type="text" > </p>
<p> 密碼:<input name="pass" type="password" > </p>
<p>
<input type="submit" name="Button" value="提交"/>
<input type="reset" name="Reset" value="重填“/>
</p>
</form>
3、表單元素說明:
type:指定元素的類型。text、password、checkbox、radio、submit、reset、file、hidden、image 和 button,默認為 text.
name:指定表單元素的名稱.
value:元素的初始值。type 為 radio時必須指定一個值.
size:指定表單元素的初始寬度。當 type 為 text 或 password時,表單元素的大小以字符為單位。對于其他類型,寬度以像素為單位.
maxlength:type為text 或 password 時,輸入的最大字符數.
checked:type為radio或checkbox時,指定按鈕是否是被選中.
4、示例:
<html >
<head>
<title>表單元素</title>
</head>
<body>
<!-- 表單 -->
<form method="POST" action="#">
<!-- 標簽 -->
<label for="username">姓名:</label>
<!-- 文本框 value屬性是設置默認顯示的值-->
<input id="username" value="songzetong" />
<!-- 密碼框 -->
<br/><label for="pwd">密碼:</label>
<input type="password" id="pwd">
<br/>
<!-- 單選框 -->
<label for="sex">性別:</label>
<input type ="radio" name ="sex" checked/>男
<input type ="radio" name ="sex"/>女
<!-- 復選框 -->
<br/>
<label for="hobby">愛好:</label>
<input type="checkbox" name ="hobby" id="hobby"/>聽音樂
<input type="checkbox" name ="hobby"/>旅游
<input type="checkbox" name ="hobby"/>游泳
<br/>
<!-- 下拉列表 -->
<label for="month">月份:</label>
<select id="month"/>
<option>1月</option>
<option>2月</option>
<option>3月</option>
</select>
<br/>
<!-- 按鈕 -->
<input type="reset" value="重置按鈕"/>
<input type="submit" value="提交按鈕"/>
<input type="button" value="普通按鈕"/>
<br/>
<!-- 圖片按鈕 -->
<input type="image" src="one.jpg" width="200px" heigth="200px"/>
<br/>
<button type="submit">提交</button>
<button type="reset">重置</button>
<br/>
<label for="profile">
個人簡介:
</label>
<!-- 多行文本域 -->
<textarea >本人已同意什么條款</textarea>
<br/>
<br/>
<br/>
<!-- 文件域 -->
<label for="upload">上傳頭像:</label>
<input type="file"/>
<!-- 郵箱 -->
<br/>
<label for="QQ郵箱">郵箱:</label>
<input type="email"/>
<br/>
<!-- 網址 -->
<label for="ur">網址:</label>
<input type="url"/>
<!-- 數字 -->
<br/>
<label for="shuzi">數字:</label>
<input type="number" name="shuzi" min="0" max="100" step="10"/>
<br/>
<label for="huakuai">滑塊:</label>
<input type="range" />
<!-- 搜索框 -->
<br/>
<label for="sousuo">搜索</label>
<input type="search"/>
<!-- 隱藏域 -->
<br/>
<input type="hidden"value="1">
<!-- 只讀:只能看不能修改,禁用:不能用 -->
<input value="我是只讀的" readonly/>
<input type="button" value="我是禁用的" disabled/>
<!-- palceholder默認提示 -->
<br/>
<input placeholder="默認提示框"/>
<br/>
<!-- 文本框內容提示不能為空,否則不允許用戶提交表單(網頁上的必填項) -->
<input required="必填項"/>
<button type="submit">提交</button>
<br/>
<!-- 用戶輸入的內容必須符合正則表達式所指的規則,否則就不能提交表單-->
<input required pattern="^1[3578]\d{9}"/>
<button type="submit">提交</button>
</form>
</body>
</html>
效果圖鏈接:file:///D:/ruanjian/VS/wenjianxiangmu/htmlThree/form.html
前面已經學習相關html大部分知識,基本上可以制作出簡單的頁面,但是這些頁面都是靜態的,一個網站如果要實現用戶的互動交流,這時表單就起到關鍵的作用,表單的用途很多,它主要用來收集用戶的相關信息,是網頁具有交互的功能。例如,用戶注冊登錄,留言等。
下面會詳細介紹表單的使用方法,有以下內容:
使用<form></form>標簽來創建一個表單,在其之間就是各種表單控件,如,輸入框,文本框,單選按鈕,多選按鈕,提交按鈕等。
語法代碼如下:
<form name="表單名稱" action="表單處理程序的服務地址" method="提交表單時所用的HTTP方法">
...... 表單控件......
</form>
1、action —— 處理程序
這里的 action 屬性值表單提交的地址,就是表單收集好數據后要傳遞給遠程服務的地址,其地址可以是絕對路徑也可以是相對路徑,或者其它形式,如發送電子郵件。
使用表單發送電子郵件:
<form action="mailto:xxx@126.com">
...... 表單控件......
</form>
提交到后臺程序地址:
<form action="action_page.php">
...... 表單控件......
</form>
2、name —— 表單名稱
表單名稱,這一屬性不是必需的,但是為了防止表單信息提交到后臺處理程序時發生混亂,一般要設置一個名稱,且在同一頁面中最好是唯一的,不要和其它表單重復命名。
<form name="loginForm">
...... 表單控件......
</form>
3、method —— 傳送數據方法
method 屬性用來定義處理程序使用那種方法來獲取數據信息,常用的有 get 和 post (http 協議定義的方法)。
<form name="loginForm" action="get 或 post">
...... 表單控件......
</form>
何時使用 GET?
GET 最適合少量數據或不是很重要數據的提交,瀏覽器會設定容量限制,默認如何沒有設置method 屬性,表單則會使用get 方法。
當您使用 GET 時,表單數據在頁面地址欄中是可見的,會在表單提交的地址后面跟一個問號“?” ,問號后面是數據,以 名稱1=值1&名稱2=值2 形式發送到后臺程序。
地址欄會看到如下:
action_page.php?firstname=Mickey&lastname=Mouse
關于 GET 的注意事項:
以名稱/值對的形式將表單數據追加到 URL
永遠不要使用 GET 發送敏感數據!(提交的表單數據在 URL 中可見!)
URL 的長度受到限制(2048 個字符)
對于用戶希望將結果添加為書簽的表單提交很有用
GET 適用于非安全數據,例如 Google 中的查詢字符串
何時使用 POST?
使用post 表單數據和url(表單提交地址)是分開發送的,在頁面地址欄中被提交的數據是不可見的,這樣安全性更好,用戶端會通知服務端獲取數據,所以這種情況沒有數據長度的限制,缺點是速度會慢些。
關于 POST 的注意事項:
將表單數據附加在 HTTP 請求的正文中(不在 URL 中顯示提交的表單數據)
POST 沒有大小限制,可用于發送大量數據。
帶有 POST 的表單提交后無法添加書簽
4、enctype —— 編碼方式
enctype 屬性規定在發送到服務器之前應該如何對表單數據進行編碼。
<form enctype="value">
有以下幾種值:
值 | 含義 |
application/x-www-form-urlencoded | 在發送前編碼所有字符(默認編碼方式) |
multipart/form-data | 不對字符編碼。 在使用包含文件上傳控件的表單時,必須使用該值。 |
text/plain | 以純文本的方式,空格轉換為 "+" 加號,但不對特殊字符編碼。 |
5、target —— 目標顯示方式
target 屬性規定在何處打開 action URL。表單的目標窗口通常用來顯示表單返回的信息,例如是否成功提交了表單,是否出錯等。
<form target="value">
屬性值有以下:
值 | 描述 |
_blank | 在新窗口中打開。 |
_self | 默認。在表單當前的窗口中打開。 |
_parent | 在父窗口中打開。 |
_top | 在頂級窗口中打開。 |
framename | 在指定的框架窗口中打開。 |
看到這里是不是想的了之前學習超鏈接時候,a標簽也有同樣的屬性,這里差不多一個意思,只是用途不一樣。
這里的窗口有可能是框架 frame 或 浮動窗口 iframe ,后面會講到框架和浮動窗口,就是在一個頁面中可以嵌套一個子窗口。
什么是表單控件,就是收集數據的各種形式控件,比如輸入框,密碼框,單選、多選按鈕,下拉菜單,文本域,文件域,提交按鈕等等。
如下代碼:
<form name="form1">
<div>用戶名:<input name="username" type="text" ></div>
<div>密碼:<input name="password" type="password" ></div>
<div>性別:
<input type="radio" name="sex" value="male" checked> 男
<br>
<input type="radio" name="sex" value="female"> 女
</div>
<div> 車輛:
<select name="cars">
<option value="volvo">baom</option>
<option value="saab">Saab</option>
<option value="fiat">Fiat</option>
<option value="audi">Audi</option>
</select>
</div>
<div>留言:
<textarea name="message" rows="10" cols="30">
請輸入文本
</textarea>
</div>
</select>
</form>
效果如下:
這里顯示了一個基本表單,包含了輸入框,密碼框,單選,下拉菜單,文本域等組件,下面會按其使用類型介紹表單控件。
所有表單控件都一個name屬性和vaule屬性,用于和其它控件區別,后臺程序將會以此名稱獲取其表單控件對應的vaule值。
下篇會介紹各種表單控件的使用,感謝關注。
上篇:前端入門——html 表格的使用
下篇:前端入門——html 表單控件使用
*請認真填寫需求信息,我們會在24小時內與您取得聯系。