1.為什么要將form表單序列化?
ajax上傳form表單的原始方式,是將form表單中所需要的鍵值對先獲取,然后再組裝成數據(兩種方式:http:localhost:8080/test.do?personName=張三&sex=1 / json的格式),這種方式有大量的form表單中的數據要獲取,序列化的方式,只用調用一個方法即可獲取上述兩種方式的數據。
2.使用jquery序列化
<form id="test-form" action="" >
<input name="person.name" value=“張三“>
<input name="person.sex" value="1“>
<!-- 測試被display修飾的元素是否會被序列化 -->
<input style="display:none;" name="person.age" value="1“>
<!-- 測試沒有name屬性的元素是否會被序列化 -->
<input value="1“>
</form>
1.獲取url帶參數形式的數據
console.info($("#test-form").serialize() ) ; //這里是使用jquery序列化的方法。
序列化之后的結果:“&person.name=張三(這里中文會被編碼)&person.sex=1&person.age=1”
由此可見被display修飾的元素也會被序列化,沒有name屬性的input框不會被序列化,因為序列化的本質是key-value的形式
2.獲取json對象
console.info($("#test-form").serializeObject());
序列化之后的結果:{"person.name":"張三","person.sex":"1","person.age"}
被display修飾的元素依然會被序列化
3.select/textarea等元素是否能被序列化?
未做測試,但是可以寫input的隱藏域來代替這是標簽