Serializejavascript
主要用於在提交表單數據時,序列表表格內容爲字符串.java
$("#form1").serialize()數組
Param數據結構
是serialize方法的核心,用來對一個數組或對象按照kkey/value進行序列化spa
$.param(obj);code
栗子:orm
<form action="#" id="form1"> name:<input type="text" id="username" name="username"><br> age:<input type="text" id="userage" name="userage"><br> <input id="send" type="button" value="提交"> </form> <script type="text/javascript"> $("#send").click(function(){ var abc={a:1,b:2}; var d=$.param(abc); alert(d);//彈出 a=1&b=2 //這段話做用和下面一種方式是同樣的 $.get("../AjaxServletDemo",$("#form1").serialize(),callback); //$.get("../AjaxServletDemo",{username:$("#username").val(),userage:$("#userage").val()},callback); }); function callback(responseText,responseStatus,xmlHttpRequest){ alert(responseText+responseStatus+xmlHttpRequest.readyState); } </script>
serializeArray()xml
序列化表格元素 (相似 '.serialize()' 方法) 返回 JSON 數據結構數據。對象
栗子:ip
<form action="#" id="form1"> <input type="checkbox" name="username" value="username1">A1<br> <input type="checkbox" name="username" value="username2">A2<br> <input type="radio" name="sex" value="MALE" >男<br> <input type="radio" name="sex" value="FEMALE">女<br> <input id="send" type="button" value="提交"> </form> <script type="text/javascript"> $("#send").click(function(){ var fields=$(":checkbox,:radio").serializeArray(); console.log(fields); $.each(fields,function(i,field){ alert(field.value);//會彈出選中數據的value值 }); })