serialize

一、serialize()定義和用法:

serialize()方法通過序列化表單值,創建標準的URL編碼文本字符串,它的操作對象是代表表單元素集合的jQuery
對象。你可以選擇一個或多個表單元素(比如input或文本框),或者form 元素本身。序列化的值可在生成AJAX 請求時用於URL
查詢字符串中。



語法:

$(selector).serialize()


詳細說明
1、.serialize()方法創建以標準URL 編碼表示的文本字符串。它的操作對象是代表表單元素集合的jQuery 對象。

2、.serialize()方法可以操作已選取個別表單元素的jQuery 對象,比如<input>, <textarea> 以及
<select>。不過,選擇<form> 標籤本身進行序列化一般更容易些

3、只會將”成功的控件“序列化爲字符串。如果不使用按鈕來提交表單,則不對提交按鈕的值序列化。如果要表單元素的值包含到序列字符串中,元素必須使用
name 屬性。



4、form裏面的name不能夠用 Js、jquery裏的關鍵字。

例如:length
<form id="form1"> <input name="length"type="text" value="pipi" />
<input name="blog" type="text" value="bluesubmarine" /> </form>
//使用:$("#form1").serialize();
上面則獲取不到值。



二、JQuery中serialize()實例



1、ajaxserialize()
$.ajax({ type: "POST", dataType: "json", url:ajaxCallBack,
data:$('#myForm').serialize(),// 要提交表單的ID success: function(msg){
alert(msg); } });


2、serialize()序列化表單實例


<script src="jquery-1.7.min.js"></script> <script>$(function(){
$("#submit").click(function(){alert($("#myForm").serialize()); }); });
</script> <form id="myForm"> 暱稱 <inputtype="text" name="username"
value="admin" /><br /> 密碼 <input type="password"name="password"
value="admin123" /><br /> <input type="button"id="submit" value="序列化表單"
/> </form>


點擊按鈕之後彈出:

username=admin&password=admin123



三、serialize是用param方法對serializeArray的一個簡單包裝



1、$.param()

$.param()方法是serialize()方法的核心,用來對一個數組或對象按照key/value進行序列化。



param方法的js代碼
param: function( a ) { /// <summary> /// This method is internal. Use
serialize() instead. /// </summary> /// <param name="a"type="Map">A map
of key/value pairs to serialize into a string.</param>' /// <returns
type="String" /> /// <private /> var s = [ ]; function add(key, value
){ s[ s.length ] = encodeURIComponent(key) + '=' +
encodeURIComponent(value); }; // If an array was passed in, assume that
it is an array // of form elements if ( jQuery.isArray(a) || a.jquery )
// Serialize the form elements jQuery.each( a, function(){ add(
this.name, this.value ); }); // Otherwise, assume that it's an object of
key/value pairs else // Serialize the key/values for ( var j in a ) //
If the value is an array then the key names need to be repeated if (
jQuery.isArray(a[j]) ) jQuery.each( a[j], function(){ add( j, this );
}); else add( j, jQuery.isFunction(a[j]) ? a[j]() : a[j] ); // Return
the resulting serialization return s.join("&").replace(/%20/g,"+"); }


例如

var obj = {a:1,b:2,c:3};

var k = $.param(obj);

 


alert(k);    //輸出a=1&b=2&c=3



2、serializeArray



serializeArray方法是將一個表單當中的各個字段序列化成一個數組

serializeArray方法的jquery定義
serializeArray: function() { /// <summary> /// Serializes all forms and
form elements but returns a JSON data structure. /// </summary> ///
<returns type="String">A JSON data structure representing theserialized
items.</returns> return this.map(function(){ return this.elements ?
jQuery.makeArray(this.elements) : this; }) .filter(function(){ return
this.name && !this.disabled && (this.checked ||
/select|textarea/i.test(this.nodeName) ||
/text|hidden|password|search/i.test(this.type)); }) .map(function(i,
elem){ var val = jQuery(this).val(); return val == null ? null :
jQuery.isArray(val) ? jQuery.map( val, function(val, i){ return {name:
elem.name, value: val}; }) : {name: elem.name, value: val}; }).get(); }


serializeArray數據例子
 [{name:username,value:中國},{name:password,value:xxx}]

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

發佈了25 篇原創文章 · 獲贊 3 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章