单选按钮+数组元素 [英] Radio buttons + array elements

查看:126
本文介绍了单选按钮+数组元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可以动态复制(使用JS)的表单,以便用户可以输入尽可能多的数据。这适用于文本输入,因为我只是将名称属性保留为相同(以 [] 结尾)并且然后当这些值发布时,它只是返回一个数组。现在我意识到这对单选按钮来说效果并不好,因为每个组的名称实际上都必须是唯一的。但从数据的角度来看,每个集合只返回一个值,所以从POST数据中检索数据不会是一个问题,它只是将我的表单功能搞砸了。这是没有办法的,是吗?我只是搞砸了,我不能使用数组?

解决方案

我刚刚解决了这个问题。 b
$ b

如果您有多组单选按钮,您仍然可以在数组中使用它们:

 < input type =radioname =radiobutton [0]value =a>< br> 
< input type =radioname =radiobutton [0]value =b>< br>
< input type =radioname =radiobutton [0]value =c>< br>
< br>
< input type =radioname =radiobutton [1]value =x>< br>
< input type =radioname =radiobutton [1]value =y>< br>
< input type =radioname =radiobutton [1]value =z>例如



当您提交假设你选择a和x,你将会得到一个数组radiobutton,它看起来像

radiobutton [0] =a;
radiobutton [1] =x;

它可以工作,因为每个组都有唯一的名称,但仍然使用数组语法。


I have a form that can be dynamically duplicated (with JS) so that the user can enter as much data as he wants. This works great for text inputs, because I just leave the name attribute the same (ending with a []) and then when the values are posted, it just returns me an array. Now I just realized that this doesn't work so well for radio buttons, because the names actually need to be unique for each set. But from the data standpoint, each set only returns one value, so retrieving the data from the POST data wouldn't be a problem, it just screws up the functionality of my form. There's no way around this, is there? I'm just screwed and I can't use arrays?

解决方案

I just solved this same problem.

If you have more than one group of radio buttons, you can still use them in arrays:

<input type="radio" name="radiobutton[0]" value="a"><br>
<input type="radio" name="radiobutton[0]" value="b"><br>
<input type="radio" name="radiobutton[0]" value="c"><br>
<br>
<input type="radio" name="radiobutton[1]" value="x"><br>
<input type="radio" name="radiobutton[1]" value="y"><br>
<input type="radio" name="radiobutton[1]" value="z">

for example.

When you submit that form, and assuming you pick "a" and "x", you will have an array "radiobutton" that looks like

radiobutton[0] = "a";
radiobutton[1] = "x";

It works because each group has a unique name, but still uses the array syntax.

这篇关于单选按钮+数组元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆