【问题标题】:How to get elements of form as array如何将表单元素作为数组获取
【发布时间】:2016-03-24 21:06:41
【问题描述】:

如何使用 fm_id.serialize() 将特定字段集的表单元素动态收集为数组 我的代码:-

  <form id="registration_form">
  <fieldset id="myFieldset1" name="">
  Name: <input type="text" name="username"><br>
  Age: <input type="text" name="age"><br>
  Place: <input type="text" name="place"><br>
  </fieldset>

  <fieldset id="myFieldset2" name="">
   Email: <input type="text" name="usermail"><br>
   Job: <input type="text" name="job"><br>
  </fieldset>
  </form>

在单击表单中的下一步按钮时滑动这些字段集。我需要在单击按钮时将每个字段集值作为数组获取以提交字段集值

【问题讨论】:

  • 你想要它在客户端吗?
  • 在客户端是

标签: php html


【解决方案1】:

在 js 中你可能会得到这样的数组形式

var form = document.registration_form;
//get form data
var data = $(form).serializeArray();

【讨论】:

  • 我不希望元素整个表单作为数组,需要一个特定的字段集作为数组。在这个例子中,我需要两个字段集的两个数组
【解决方案2】:

您可以很好地序列化字段集。

$("#myFieldset1").change(function(){
  $("#out").text($("#myFieldset1").serialize());
});
$("#myFieldset2").change(function(){
  $("#out").text($("#myFieldset2").serialize());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="registration_form">
  <fieldset id="myFieldset1" name="">
  Name: <input type="text" name="username"><br>
  Age: <input type="text" name="age"><br>
  Place: <input type="text" name="place"><br>
  </fieldset>

  <fieldset id="myFieldset2" name="">
   Email: <input type="text" name="usermail"><br>
   Job: <input type="text" name="job"><br>
  </fieldset>
  </form>
<pre id="out"></pre>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-06
    • 1970-01-01
    • 2013-06-30
    • 1970-01-01
    • 1970-01-01
    • 2016-10-02
    • 1970-01-01
    • 2018-07-07
    相关资源
    最近更新 更多