JQuery中serialize()用法實(shí)例分析
本文實(shí)例講述了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
<input name="length" type="text" value="pipi" />
<input name="blog" type="text" value="blue submarine" />
</form>
//使用:$("#form1").serialize();
上面則獲取不到值。
二、JQuery中serialize()實(shí)例
1、ajax serialize()
type: "POST",
dataType: "json",
url:ajaxCallBack,
data:$('#myForm').serialize(),// 要提交表單的ID
success: function(msg){
alert(msg);
}
});
2、serialize() 序列化表單實(shí)例
<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代碼
/// <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 k = $.param(obj);
alert(k); //輸出a=1&b=2&c=3
2、serializeArray
serializeArray方法是將一個表單當(dāng)中的各個字段序列化成一個數(shù)組
serializeArray方法的jquery定義
/// <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
}]
希望本文所述對大家的jQuery程序設(shè)計(jì)有所幫助。
- jQuery Form 表單提交插件之formSerialize,fieldSerialize,fieldValue,resetForm,clearForm,clearFields的應(yīng)用
- jQuery中serializeArray()與serialize()的區(qū)別實(shí)例分析
- jQuery基于ajax()使用serialize()提交form數(shù)據(jù)的方法
- JQuery中serialize() 序列化
- JQuery中serialize()、serializeArray()和param()方法示例介紹
- jQuery ajax中使用serialize()方法提交表單數(shù)據(jù)示例
- jQuery ajax serialize()方法的使用以及常見問題解決
- jQuery-serialize()輸出序列化form表單值的方法
- 與jquery serializeArray()一起使用的函數(shù),主要來方便提交表單
- jQuery使用serialize()表單序列化時出現(xiàn)中文亂碼問題的解決辦法
相關(guān)文章
jquery 動態(tài)遍歷select 賦值的實(shí)例
今天小編就為大家分享一篇jquery 動態(tài)遍歷select 賦值的實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09
jQuery中的height innerHeight outerHeight區(qū)別示例介紹
這篇文章主要介紹了jQuery中的height innerHeight outerHeight的區(qū)別,需要的朋友可以參考下2014-06-06
jQuery獲取所有父級元素及同級元素及子元素的方法(推薦)
這篇文章主要介紹了jQuery獲取所有父級元素及同級元素及子元素的方法,本文給大家介紹的非常詳細(xì),具有參考借鑒價(jià)值 ,需要的朋友可以參考下2018-01-01
jquery ui 1.7 ui.tabs 動態(tài)添加與關(guān)閉(按鈕關(guān)閉+雙擊關(guān)閉)
jquery ui 1.7 ui.tabs 動態(tài)添加與關(guān)閉(按鈕關(guān)閉+雙擊關(guān)閉)實(shí)現(xiàn)代碼,需要的朋友可以參考下。2010-04-04
jQuery簡單實(shí)現(xiàn)根據(jù)日期計(jì)算星期幾的方法
這篇文章主要介紹了jQuery簡單實(shí)現(xiàn)根據(jù)日期計(jì)算星期幾的方法,涉及jQuery針對日期時間簡單計(jì)算相關(guān)操作技巧,需要的朋友可以參考下2019-01-01
jQuery圖片切換插件jquery.cycle.js使用示例
Cycle供了非常好的功能來幫助大家更簡單的使用插件的幻燈功能,下面是它的一個非常不錯的示例,大家可以學(xué)習(xí)下2014-06-06

