JQuery中serialize() 序列化
本文導(dǎo)讀:在jQuery中,當(dāng)我們使用ajax時,常常需要拼裝input數(shù)據(jù)以鍵值對(Key/Value)的形式發(fā)送到服務(wù)器,用JQuery的serialize方法可以輕松的完成這個工作,使用這個方法可以將表單序列化為鍵值對(key1=value1&key2=value2…)后提交。下面介紹JQuery中serialize()的用法
一、serialize()定義和用法:
serialize()方法通過序列化表單值,創(chuàng)建標(biāo)準(zhǔn)的URL編碼文本字符串,它的操作對象是代表表單元素集合的jQuery 對象。你可以選擇一個或多個表單元素(比如input或文本框),或者 form 元素本身。序列化的值可在生成 AJAX 請求時用于 URL 查詢字符串中。
語法:
$(selector).serialize()
詳細(xì)說明
1、.serialize() 方法創(chuàng)建以標(biāo)準(zhǔn) URL 編碼表示的文本字符串。它的操作對象是代表表單元素集合的 jQuery 對象。
2、.serialize() 方法可以操作已選取個別表單元素的 jQuery 對象,比如 <input>, <textarea> 以及 <select>。不過,選擇 <form> 標(biāo)簽本身進(jìn)行序列化一般更容易些
3、只會將”成功的控件“序列化為字符串。如果不使用按鈕來提交表單,則不對提交按鈕的值序列化。如果要表單元素的值包含到序列字符串中,元素必須使用 name 屬性。
4、form里面的name不能夠用 Js、jquery里的關(guān)鍵字。
例如:length
<form id="form1">
<input name="length" type="text" value="pipi" />
<input name="blog" type="text" value="blue submarine" />
</form>
//使用:$("#form1").serialize();
上面則獲取不到值。
二、JQuery中serialize()實(shí)例
1、ajax serialize()
$.ajax({
type: "POST",
dataType: "json",
url:ajaxCallBack,
data:$('#myForm').serialize(),// 要提交表單的ID
success: function(msg){
alert(msg);
}
});
2、serialize() 序列化表單實(shí)例
<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>
點(diǎn)擊按鈕之后彈出:
username=admin&password=admin123
三、serialize是用param方法對serializeArray的一個簡單包裝
1、$.param()
$.param()方法是serialize()方法的核心,用來對一個數(shù)組或?qū)ο蟀凑誯ey/value進(jìn)行序列化。
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方法是將一個表單當(dāng)中的各個字段序列化成一個數(shù)組
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數(shù)據(jù)例子
[ { name : username, value : 中國 }, { name : password, value : xxx }]
以上就是本文所述的全部內(nèi)容了,希望大家能夠喜歡。
相關(guān)文章
JavaScript實(shí)現(xiàn)即時通訊的 4 種方案
這篇文章主要給大家分享了JavaScript實(shí)現(xiàn)即時通訊的 4 種方案,其實(shí)就是服務(wù)端如何將數(shù)據(jù)推送到瀏覽器,下面詳細(xì)的文章內(nèi)容,需要的小伙伴參考一下,洗碗給對你有所幫助2022-02-02微信小程序?qū)崿F(xiàn)錨點(diǎn)定位功能的方法實(shí)例
“錨點(diǎn)”功能在實(shí)際應(yīng)用設(shè)計的好,可以提高用戶體驗(yàn),這篇文章主要給大家介紹了關(guān)于微信小程序?qū)崿F(xiàn)錨點(diǎn)定位功能的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2021-07-07微信小程序WebSocket實(shí)現(xiàn)聊天對話功能
這篇文章主要為大家詳細(xì)介紹了微信小程序WebSocket實(shí)現(xiàn)聊天對話功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-07-07