標籤:style blog http color java 使用 os strong
serialize:
序列表表格內容為字串,用於 Ajax 請求。可以對整個form,也可以只針對某部分。
HTML 程式碼:
<p id="results"><b>Results: </b> </p><form> <select name="single"> <option>Single</option> <option>Single2</option> </select> <select name="multiple" multiple="multiple"> <option selected="selected">Multiple</option> <option>Multiple2</option> <option selected="selected">Multiple3</option> </select><br/> <input type="checkbox" name="check" value="check1"/> check1 <input type="checkbox" name="check" value="check2" checked="checked"/> check2 <input type="radio" name="radio" value="radio1" checked="checked"/> radio1 <input type="radio" name="radio" value="radio2"/> radio2</form>
jQuery 代碼:
$("#results").append( "<tt>" + $("form").serialize() + "</tt>" );
$(‘#form‘).submit(function(event){
event.preventDefault();
$.ajax({
url:‘ ‘,
type:‘post‘,
data:$("form").serialize(),
}
參考:http://api.jquery.com/serialize/
serializeArray():
Encode a set of form elements as an array of names and values.
定義和用法
serializeArray() 方法通過序列化表單值來建立對象數組(名稱和值)。
您可以選擇一個或多個表單元素(比如 input 及/或 textarea),或者 form 元素本身。
文法
$(selector).serializeArray()
serializeArray() 方法序列化表單元素(類似 .serialize() 方法),返回 JSON 資料結構資料。
注意:此方法返回的是 JSON 對象而非 JSON 字串。需要使用外掛程式或者第三方庫進行字串化操作。
返回的 JSON 對象是由一個對象數組組成的,其中每個對象包含一個或兩個名值對 —— name 參數和 value 參數(如果 value 不為空白的話)。舉例來說:
[ {name: ‘firstname‘, value: ‘Hello‘}, {name: ‘lastname‘, value: ‘World‘}, {name: ‘alias‘}, // 值為空白]
.serializeArray() 方法使用了 W3C 關於 successful controls(有效控制項) 的標準來檢測哪些元素應當包括在內。特別說明,元素不能被禁用(禁用的元素不會被包括在內),並且元素應當有含有 name 屬性。提交按鈕的值也不會被序列化。檔案選擇元素的資料也不會被序列化。
該方法可以對已選擇單獨表單元素的對象進行操作,比如 <input>, <textarea>, 和 <select>。不過,更方便的方法是,直接選擇 <form> 標籤自身來進行序列化操作。
$("form").submit(function() { console.log($(this).serializeArray()); return false;});
上面的代碼產生下面的資料結構(假設瀏覽器支援 console.log):
[ { name: a value: 1 }, { name: b value: 2 }, { name: c value: 3 }, { name: d value: 4 }, { name: e value: 5 }]
取得表單內容並插入到網頁中:
<p><b>Results:</b> <span id="results"></span></p><form> <select name="single"> <option>Single</option> <option>Single2</option> </select> <select name="multiple" multiple="multiple"> <option selected="selected">Multiple</option> <option>Multiple2</option> <option selected="selected">Multiple3</option> </select> <br> <input type="checkbox" name="check" value="check1" id="ch1"> <label for="ch1">check1</label> <input type="checkbox" name="check" value="check2" checked="checked" id="ch2"> <label for="ch2">check2</label> <input type="radio" name="radio" value="radio1" checked="checked" id="r1"> <label for="r1">radio1</label> <input type="radio" name="radio" value="radio2" id="r2"> <label for="r2">radio2</label></form> <script> function showValues() { var fields = $( ":input" ).serializeArray(); $( "#results" ).empty(); jQuery.each( fields, function( i, field ) { $( "#results" ).append( field.value + " " ); }); } $( ":checkbox, :radio" ).click( showValues ); $( "select" ).change( showValues ); showValues();</script> </body></html>
$(":input") 選擇所有的表單輸入元素,包括input, textarea, select 和 button.
參考:
http://www.w3school.com.cn/jquery/ajax_serializearray.asp
http://api.jquery.com/serializeArray/
$.params()
序列化一個 key/value 對象:
var params = { width:1900, height:1200 };var str = jQuery.param(params);$("#results").text(str);
結果:
width=1680&height=1050
參考:http://www.w3school.com.cn/jquery/ajax_param.asp