欢迎来到DIVCSS5查找CSS资料与学习DIV CSS布局技术!
    .serializeArray()方法使用标准的W3C"successful controls"的标准来检测哪些元素应当包括在内。被禁用的元素不会被包括在内。并且,元素必须含有 name 属性。此外,提交按钮的值也不会被序列化。文件选择元素的数据也不会被序列化。
 
    .serializeArray() 方法可以对单独选择的表单元素对象进行操作, 比如 <input>, <textarea>, 和 <select>。还有个更方便的方法是,直接使用 <form> 标签来进行序列化操作:
 
$('form').submit(function() {
  console.log($(this).serializeArray());
  return false;
});
    这将产生以下数据结构(浏览器提供的console.log):
 
[
  {
    name: "a",
    value: "1"
  },
  {
    name: "b",
    value: "2"
  },
  {
    name: "c",
    value: "3"
  },
  {
    name: "d",
    value: "4"
  },
  {
    name: "e",
    value: "5"
  }
]
例子:
从表单获取值,遍历并且显示他们
 
<!DOCTYPE html>
<html>
<head>
  <style>
  body, select { font-size:14px; }
  form { margin:5px; }
  p { color:red; margin:5px; }
  b { color:blue; }
  </style>
  <script src="https://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
  <p><b>Results:</b> <span id="results"></span></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" id="ch1"/>
 
    <label for="ch1">check1</label>
    <input type="checkbox" name="check" value="check2" checked="checked" id="ch2"/>
 
    <label for="ch2">check2</label>
    <input type="radio" name="radio" value="radio1" checked="checked" id="r1"/>
 
    <label for="r1">radio1</label>
    <input type="radio" name="radio" value="radio2" id="r2"/>
 
    <label for="r2">radio2</label>
  </form>
<script>
  function showValues() {
    var fields = $(":input").serializeArray();
    $("#results").empty();
    jQuery.each(fields, function(i, field){
      $("#results").append(field.value + " ");
    });
  }
 
  $(":checkbox, :radio").click(showValues);
  $("select").change(showValues);
  showValues();
</script>
 
</body>
</html>

如需转载,请注明文章出处和来源网址:http://www.divcss5.com/html/h63942.shtml