認識表單
- 在一個頁面上可以有多個form表單,但是向web服務器提交表單的時候,一次只可以提交一個表單。
- 要聲明一個表單,只需要使用 form 標記來標明表單的開始和結束,若需要向服務器提交數據,則在form標簽中需要設置action屬性(用來設置提交表單的位置)、method屬性(用來定義瀏覽器將表單中的信息提交給服務器端程序的處理方式)、target屬性(用來指定服務器返回結果所顯示的目標窗口或目標框架);但是,對于客戶端腳本編程來說,并不需要這些屬性來幫助提交表單信息,form標簽存在的意義是在于方便在腳本中編程的時候進行引用。
- 表單的引用可以利用from標簽的name屬性或者也可以利用document的forms[]數組中調用到對應的數組。
- 可以利用form表單的elements[]數組來遍歷除了<input type=image >元素之外的所有元素
- form表單的submit()方法用于將表單提交給服務,但我們點擊submit按鈕的時候,submit按鈕會相應的調用onsubmit事件處理器從而調用Form對象的submit事件
- 如何在瀏覽器中使用頁面中的信息,我們稱之為“客戶端腳本編程”,而如何把信息提交給Web服務,并將數據庫保存在數據庫中,我們通常稱為“服務器端腳本編程”
- 在早期,所有可交互的HTML元素都應該放在HTML表單中,但是現在的定義是,需要提交到web服務器的數據,才必須要放在表單內,可是前一種理解的方式也不是完全錯誤的,因為一般可以交互的HTML元素,都是表單元素(在前期),即:瀏覽器需要處理的數據都是表單元素,因此需要將其放在HTML表單中。
認識表單元素
- 大部分的表單控件元素都是由<input>標記創建的,<input>標記具有一個type屬性,該屬性決定了<input>標記所創建的表單控件的類型。
- 所有的單控件對象都具有一個 nam e屬性,JavaScript 腳本通過 name 屬性的值來引用特定的表單控件元素,同時這也是表單提交到服務器時,每個表單控件元素的值 value 所對應的 key 值。
- 絕大部分對象都具有 value 屬性,該屬性返回當前表單控件的值。
- 所有的表單控件對象都具有一個 form 屬性,該屬性返回包含當前控件的 Form 對象。對于一個通用的表單數據檢查程序來說,用這個屬性來標明哪些控件屬于哪個表單。
- 所有的表單元素對象都具有focus()和blur()方法,同時所有的表單元素對象還具有onfocus和onblur事件處理器。
表單元素的分類
常見的表單控件有:
? ? ? ? ? ? ? 1、Text Input Elements:<input type="text"> 、<input type="password">、<textarea></textarea>
2、Tick Box Elements:<input type="checkbox"> 、<input type="radio">
3、Select Elements:<select size=1><option></option></select>、<select size=5 multiple><option></option></select>(下拉大框、多選)
4、Button:<input type="button">、<input type="submit">、<input type="reset">
表單form屬性
屬性 | 描述 |
---|---|
accept-charset | 規定在被提交表單中使用的字符集(默認:頁面字符集)。 |
action | 規定向何處提交表單的地址(URL)(提交頁面)。 |
autocomplete | 規定瀏覽器應該自動完成表單(默認:開啟)。 |
enctype | 規定被提交數據的編碼(默認:url-encoded)。 |
method | 規定在提交表單時所用的 HTTP 方法(默認:GET)。 |
name | 規定識別表單的名稱(對于 DOM 使用:document.forms.name)。 |
novalidate | 規定瀏覽器不驗證表單。 |
target | 規定 action 屬性中地址的目標(默認:_self)。 |
input
<input type=""> 元素會根據不同的?type?屬性,變化為多種形態。
type屬性值 | 表現形式 | 對應代碼 |
---|---|---|
text | 單行輸入文本 | <input type=text" /> |
password | 密碼輸入框 | <input type="password"? /> |
date | 日期輸入框 | <input type="date" /> |
checkbox | 復選框 | 我喜歡自行車:<input type="checkbox" name="Bike"> 備注:name 可以不同 |
radio | 單選框 | 男性:<input type="radio" checked="checked" name="Sex" value="male" /> 備注:name 必須一樣 |
submit | 提交按鈕 | <input type="submit" value="提交" /> |
reset | 重置按鈕 | <input type="reset" value="重置"? /> |
button | 普通按鈕 | <input type="button" value="普通按鈕"? /> |
hidden | 隱藏輸入框 | <input type="hidden"? /> |
file | 文本選擇框 | <input type="file" method='post' enctype='multipart/form-data'? /> |
屬性說明
- name:表單提交時的“鍵”,注意和id的區別
- value:表單提交時對應項的值
- type 是 "button", "reset", "submit"時,為按鈕上顯示的文本內容
- type 是 "text","password","hidden"時,為輸入框的初始值
- type 是 "checkbox", "radio", "file",為輸入相關聯的值
- checked:radio和checkbox默認被選中的項
- readonly:text和password設置只讀
- disabled:所有input均適用
select標簽
<form action="" method="post"><select name="city" id="city" multiple=true><option value="1">北京</option><option selected="selected" value="2">上海</option><option value="3">廣州</option><option value="4">深圳</option></select>
</form>
屬性說明
- multiple:布爾屬性,設置后為多選,否則默認單選
- disabled:禁用
- selected:默認選中該項
- value:定義提交時的選項值
label標簽
定義:
<label> 標簽為 input 元素定義標注(標記)。
說明:
- label 元素不會向用戶呈現任何特殊效果。
- <label> 標簽的 for 屬性值應當與相關元素的 id 屬性值相同。
<form action=""><label for="username">用戶名</label><input type="text" id="username" name="username">
</form>// 也可以寫成這樣
<form><input type='text' id='username' name='username'>
</form>
textarea多行文本
<textarea name="memo" id="memo" cols="30" rows="10">默認內容</textarea>
屬性說明
- name:名稱
- rows:行數
- cols:列數
- disabled:禁用
jQuery 操作 from
input 的文本框
// 獲取值
$("#txt").attr("value");
$("#txt").val()// 設置值
$("#txt").attr("value",'');//清空內容
$("#txt").attr("value",'11');//填充內容
input 的多選框?checkbox
// 獲取值
$("#chk1").attr("value");// 設置值,所有的jquery版本都可以這樣賦值
$("#chk1").attr("checked",''); //不打勾
$('#chk1').attr("checked", false); //不打勾
$("#chk2").attr("checked",true); //打勾
$("#chk2").attr("checked","checked"); //打勾// 設置值,jquery1.6+:prop的4種賦值,強推下列的賦值方法,prop()函數的結果:匹配到的是屬性;attr()函數的結果:匹配到的是屬性值;
$("#cb1″).prop("checked",true);
$("#cb1″).prop("checked",false);
$("#cb1″).prop({checked:true});
$("#cb1″).prop({checked:false}); // 判斷是否已經打勾
if ($("#chk1").attr('checked')==undefined){} //看版本1.6+返回:”checked”或”undefined” ;1.5-返回:true或false
if ($("#chk1").is(':checked')){} //所有版本:true/false//別忘記冒號哦
if ($("#chk1").get(0).checked) {}
if ($("#chk1")[0].checked) {}
if ($('#chk1').prop('checked')) {} //16+:true/false//設置checkbox為禁用狀態(jquery<1.6用attr,jquery>=1.6建議用prop)
$("input[type='checkbox']").attr("disabled", "disabled");//or
$("input[type='checkbox']").attr("disabled", true);//or
$("input[type='checkbox']").prop("disabled", true);//or
$("input[type='checkbox']").prop("disabled", "disabled");//設置checkbox為啟用狀態(jquery<1.6用attr,jquery>=1.6建議用prop)
$("input[type='checkbox']").removeAttr("disabled");//or
$("input[type='checkbox']").attr("disabled", false);//or
$("input[type='checkbox']").prop("disabled", "");//or
$("input[type='checkbox']").prop("disabled", false);
input 的單選框?radio
//獲取radio被選中項的值
$('input:radio:checked').val();
$("input[type='radio']:checked").val();
$("input[name='items']:checked").val();
$(“input[@name=items]:checked”).val();
$("input[@type=radio][@checked]").val();// radio單選組的第二個元素為當前選中值
$('input:radio').eq(索引值).attr('checked', 'true');索引值=0,1,2....
$('input:radio').slice(1,2).attr('checked', 'true');
$('input[@name=items]').get(1).checked = true;//設置value=2的項目為當前選中項
$("input[@type=radio]").attr("checked",'2');
$("input:radio[value=http://www.2cto.com/kf/201110/'rd2']").attr('checked','true');
$("input[value=http://www.2cto.com/kf/201110/'rd2']").attr('checked','true');// 設置第一個Radio為選中值:
$('input:radio:first').attr('checked', 'checked');
$('input:radio:first').attr('checked', 'true');
$('input:radio:first').attr('checked', true);
注:attr("checked",'checked')= attr("checked", 'true')= attr("checked", true)// 刪除Value值為rd2的Radio
$("input:radio[value=http://www.2cto.com/kf/201110/'rd2']").remove();// 刪除第幾個Radio
$("input:radio").eq(索引值).remove();索引值=0,1,2....// 如刪除第3個Radio:
$("input:radio").eq(2).remove();//遍歷Radio
$('input:radio').each(function(index,domEle){//寫入代碼
});
select 標簽
// 獲取選中項
$('#sel option:selected').val();
$('select#sel').find('option:selected').val();// 獲取選中項的Text值:
$('select#seloption:selected').text();
$('select#sel').find('option:selected').text();
$("select[@name=items] option[@selected]").text();// 設置第一個option為選中值:
$('select#sel option:first').attr('selected','true')
$('select#sel')[0].selectedIndex = 0;
$("#sel").attr("value",'-sel3');//設置value=-sel3的項目為當前選中項
$("<optionvalue='1'>1111</option><optionvalue='2'>2222</option>")
.appendTo("#sel")//添加下拉框的option
$("#sel").empty();//清空下拉框// 獲取當前選中項的索引值:
$('select#sel').get(0).selectedIndex;// 獲取當前option的最大索引值:
$('select#sel option:last').attr("index")// 獲取DropdownList的長度:
$('select#sel')[0].options.length;
$('select#sel').get(0).options.length;
?