JQuery中serialize()用法執行個體分析

來源:互聯網
上載者:User

JQuery中serialize()用法執行個體分析

 這篇文章主要介紹了JQuery中serialize()用法,執行個體分析了serialize()方法的功能、定義及相關提示,需要的朋友可以參考下

 

 

本文執行個體講述了JQuery中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="blue submarine" />
</form>

 

//使用:$("#form1").serialize();

 

上面則擷取不到值。

二、JQuery中serialize()執行個體

1、ajax serialize()

代碼如下:

$.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">
暱稱 <input type="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 the serialized 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
}]

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.