[JQuery]serialize()和serializeArray()

1、serialize()把表单的值序列化成字符串

 <html>
 <head>
     <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
 </head>
 <body>
 <p id="results"><b>Results: </b> </p>
 <form>
     <select name="single-name">
         <!--这一段序列化为字符串的结果是:name=text 在这里,默认选中的是Single1——> single-name=Single1-->
         <option>Single1</option>
         <option>Single2</option>
     </select>
     <select name="multiple-name" multiple="multiple">
         <!--这一段序列化为字符串的结果是:name=text 在这里——> multiple-name=Multiple1&multiple-name=Multiple3-->
         <option selected="selected">Multiple1</option>
         <option>Multiple2</option>
         <option selected="selected">Multiple3</option>
     </select><br/>
     <input type="checkbox" name="check-name" value="check1" checked="checked"/> check1111
     <!--这一段序列化为字符串的结果是:name=value——>check-name=check1-->
     <input type="checkbox" name="check-name" value="check2" checked="checked"/> check2222
     <!--这一段序列化为字符串的结果是:name=value——>chec-namek=check2-->
     <input type="radio" name="radio-name" value="radio1" checked="checked"/> radio1111
     <!--这两段只能同时选中一个,序列化为字符串的结果是:name=value——>radio-name=radio1-->
     <input type="radio" name="radio-name" value="radio2" checked="checked"/> radio2222
 </form>
 <script>
     $("#results").append( "<tt>" + $("form").serialize() + "</tt>" );
 </script>
 </body>
 </html>
最终序列化成字符串的结果是:single-name=Single1&multiple-name=Multiple1&multiple-name=Multiple3&check-name=check1&check-name=check2&radio-name=radio2

2、serializeArray()把表单的值序列化成JSON对象,是对象哦
 <html>
 <head>
     <meta charset="utf-8">
     <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
 </head>
 <body>
 <p id="results"><b>Results: </b> </p>

 <form>
     <select name="single">
         <option>Single</option>
         <option>Single2</option>
     </select>
     <select name="multiple" multiple="multiple">
         <option selected="selected">Multiple</option>
         <option>Multiple2</option>
         <option selected="selected">Multiple3</option>
     </select><br/>
     <input type="checkbox" name="check" value="check1"/> check1
     <input type="checkbox" name="check" value="check2" checked="checked"/> check2
     <input type="radio" name="radio" value="radio1" checked="checked"/> radio1
     <input type="radio" name="radio" value="radio2"/> radio2
 </form>
 <script>
 var fields = $("select, :radio").serializeArray();
 jQuery.each( fields, function(i, field){
     alert(JSON.stringify(field));
     $("#results").append(field.value +" ");
 });
 </script>
 </body>
 </html>
上一篇:按钮制作技巧(css精灵效果)-高级版


下一篇:checking for oracle home incompatibilities failed