将选择框分组到数组中进行发布?

发布于 2024-08-15 04:17:37 字数 425 浏览 5 评论 0原文

我知道如何通过多选表单对象发送数据并将数据分组到一个数组中:

<select name="my_data[]" multiple="multiple"/>

是否可以有多个具有“单个”值的不同选择框并将它们全部推入一个数组中?即

<select id="select-1" name="my_data[]"/>
<select id="select-2" name="my_data[]"/>

结果将是

[
    0 => {value of select-1},
    1 => {value of select-2}
]

如果不可能的话,将选择的数据组合到数组中的好方法是什么?

I know how to send data via a multi-select form object and group the data into an array:

<select name="my_data[]" multiple="multiple"/>

Is it possible to have multiple different select boxes with "single" values and push them all into an array? i.e.

<select id="select-1" name="my_data[]"/>
<select id="select-2" name="my_data[]"/>

result would be

[
    0 => {value of select-1},
    1 => {value of select-2}
]

What would be a good way to combine the data from the selects into an array if not possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

写给空气的情书 2024-08-22 04:17:37

啊,仅仅删除多个似乎就可以了。当表单元素名称中包含“[]”时,Zend Framework 的 FormSelect 帮助程序会自动添加它,但我没有意识到这一点。

Ah, just removing multiple seems to work. Zend Framework's FormSelect helper automatically added it when you have "[]" in your form element name and I did not realize that.

幼儿园老大 2024-08-22 04:17:37

假设您正在获取页面上的所有

// get all selects
var boxes = document.getElementsByTagName("select"),
    arr = []; // your final values array

// for each select, pull out the value and push it into 'arr'
for(var i = 0, len = boxes.length; i < len, i++) {
  arr.push(boxes[i].value);
}

This assume you're getting all <select>'s on the page:

// get all selects
var boxes = document.getElementsByTagName("select"),
    arr = []; // your final values array

// for each select, pull out the value and push it into 'arr'
for(var i = 0, len = boxes.length; i < len, i++) {
  arr.push(boxes[i].value);
}
走过海棠暮 2024-08-22 04:17:37

虽然你可能可以做到,但我不推荐它。只是因为其他人阅读代码并不清楚。

更好的方法就是在服务器端将它们组合起来。假设:

<select id="select-1" name="data_1[]"/>
...

<select id="select-2" name="data_2[]"/>
...

在 PHP 端:

$data1 = $_POST['data_1'];
$data2 = $_POST['data_2'];
$combined = array_merge($data1, $data2);

While you probably can do it I wouldn't recommend it. Just because it isn't that clear from someone else reading the code.

A better approach is simply to combine them serverside. Assuming:

<select id="select-1" name="data_1[]"/>
...

<select id="select-2" name="data_2[]"/>
...

On the PHP side:

$data1 = $_POST['data_1'];
$data2 = $_POST['data_2'];
$combined = array_merge($data1, $data2);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文