加载中...

serializeArray()


概述    .serializeArray()

返回值:Array<Object>

描述:将用作提交的表单元素的值编译成拥有name和value对象组成的数组。例如[ { name: a value: 1 }, { name: b value: 2 },...]

  • V : 1.2.serializeArray()

    • 这个方法不接受任何参数。

.serializeArray() 方法创建一个对象组成的javascript数组,用来编码成一个JSON一样的字符串。 它对表单 和/或 表单控件的 jQuery 集合起作用。该控件可以有以下几种类型:

  1. <form>
  2. <div><input type="text" name="a" value="1" id="a" /></div>
  3. <div><input type="text" name="b" value="2" id="b" /></div>
  4. <div><input type="hidden" name="c" value="3" id="c" /></div>
  5. <div>
  6. <textarea name="d" rows="8" cols="40">4</textarea>
  7. </div>
  8. <div><select name="e">
  9. <option value="5" selected="selected">5</option>
  10. <option value="6">6</option>
  11. <option value="7">7</option>
  12. </select></div>
  13. <div>
  14. <input type="checkbox" name="f" value="8" id="f" />
  15. </div>
  16. <div>
  17. <input type="submit" name="g" value="Submit" id="g" />
  18. </div>
  19. </form>

.serializeArray()方法使用标准的W3C"successful controls"的标准来检测哪些元素应当包括在内。被禁用的元素不会被包括在内。并且,元素必须含有 name 属性。此外,提交按钮的值也不会被序列化。文件选择元素的数据也不会被序列化。

.serializeArray() 方法可以对单独选择的表单控件的jQuery对象进行操作, 比如 <input>, <textarea>, 和 <select>。还有个更方便的方法是,直接使用 <form> 标签来进行序列化操作:

  1. $('form').submit(function() {
  2. console.log($(this).serializeArray());
  3. return false;
  4. });

这将产生以下数据结构(浏览器提供的console.log):

  1. [
  2. {
  3. name: "a",
  4. value: "1"
  5. },
  6. {
  7. name: "b",
  8. value: "2"
  9. },
  10. {
  11. name: "c",
  12. value: "3"
  13. },
  14. {
  15. name: "d",
  16. value: "4"
  17. },
  18. {
  19. name: "e",
  20. value: "5"
  21. }
  22. ]

示例

实例

取得表单内容并插入到网页中。

HTML 代码:
  1. <p id="results"><b>Results:</b> </p>
  2. <form>
  3. <select name="single">
  4. <option>Single</option>
  5. <option>Single2</option>
  6. </select>
  7. <select name="multiple" multiple="multiple">
  8. <option selected="selected">Multiple</option>
  9. <option>Multiple2</option>
  10. <option selected="selected">Multiple3</option>
  11. </select><br/>
  12. <input type="checkbox" name="check" value="check1"/> check1
  13. <input type="checkbox" name="check" value="check2" checked="checked"/> check2
  14. <input type="radio" name="radio" value="radio1" checked="checked"/> radio1
  15. <input type="radio" name="radio" value="radio2"/> radio2
  16. </form>
jQuery 代码:
  1. var fields = $("select, :radio").serializeArray();
  2. jQuery.each( fields, function(i, field){
  3. $("#results").append(field.value + " ");
  4. });

描述:

从表单获取值,遍历并且显示他们

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. body, select { font-size:14px; }
  6. form { margin:5px; }
  7. p { color:red; margin:5px; }
  8. b { color:blue; }
  9. </style>
  10. <script src="http://code.jquery.com/jquery-latest.js"></script>
  11. </head>
  12. <body>
  13. <p><b>Results:</b> <span id="results"></span></p>
  14. <form>
  15. <select name="single">
  16. <option>Single</option>
  17. <option>Single2</option>
  18. </select>
  19. <select name="multiple" multiple="multiple">
  20. <option selected="selected">Multiple</option>
  21. <option>Multiple2</option>
  22. <option selected="selected">Multiple3</option>
  23. </select><br/>
  24. <input type="checkbox" name="check" value="check1" id="ch1"/>
  25. <label for="ch1">check1</label>
  26. <input type="checkbox" name="check" value="check2" checked="checked" id="ch2"/>
  27. <label for="ch2">check2</label>
  28. <input type="radio" name="radio" value="radio1" checked="checked" id="r1"/>
  29. <label for="r1">radio1</label>
  30. <input type="radio" name="radio" value="radio2" id="r2"/>
  31. <label for="r2">radio2</label>
  32. </form>
  33. <script>
  34. function showValues() {
  35. var fields = $(":input").serializeArray();
  36. $("#results").empty();
  37. jQuery.each(fields, function(i, field){
  38. $("#results").append(field.value + " ");
  39. });
  40. }
  41. $(":checkbox, :radio").click(showValues);
  42. $("select").change(showValues);
  43. showValues();
  44. </script>
  45. </body>
  46. </html>
运行一下


还没有评论.