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è)計有所幫助。
- 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 Animation實(shí)現(xiàn)CSS3動畫示例介紹
jQuery Animation的工作原理是通過將元素的CSS樣式從一個狀態(tài)改變?yōu)榱硪粋€狀態(tài),下面以一個實(shí)例為大家詳細(xì)介紹下具體的實(shí)現(xiàn),感興趣的朋友可以參考下2013-08-08jQuery.extend()、jQuery.fn.extend()擴(kuò)展方法示例詳解
這篇文章主要介紹了jQuery.extend()、jQuery.fn.extend()擴(kuò)展方法的應(yīng)用,需要的朋友可以參考下2014-05-05jQuery實(shí)現(xiàn)漸變下拉菜單的簡單方法
這篇文章主要介紹了jQuery實(shí)現(xiàn)漸變下拉菜單的簡單方法,涉及jQuery鏈?zhǔn)讲僮骷癱ss樣式的技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-03-03淺析ajax請求json數(shù)據(jù)并用js解析(示例分析)
這應(yīng)該是每個web開發(fā)的人員都應(yīng)該掌握的基礎(chǔ)技術(shù),需要的朋友可以參考下2013-07-07jquery實(shí)現(xiàn)帶縮略圖的可定制高度畫廊效果(5種)
這篇文章主要介紹了jquery可定制高度畫廊效果,很有藝術(shù)感,功能實(shí)現(xiàn)非常簡單,推薦給大家,有需要的小伙伴可以參考下。2015-08-08jquery之基本選擇器practice(實(shí)例講解)
下面小編就為大家?guī)硪黄猨query之基本選擇器practice(實(shí)例講解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-09-09jquery+css3實(shí)現(xiàn)會動的小圓圈效果
這篇文章主要介紹了jquery+css3實(shí)現(xiàn)會動的小圓圈效果,涉及jquery基于時間函數(shù)動態(tài)操作頁面元素css3樣式的相關(guān)技巧,需要的朋友可以參考下2016-01-01