简体   繁体   English

单选按钮阵列

[英]Radio button array

I have a form where at some point you can add multiple rows of the same question. 我有一个表单,在某些时候你可以添加同一个问题的多行。 Simplified version looks like this: 简化版看起来像这样:

<input type="text" name="name[]" value=""/>
<input type="radio" name="opted[]" value="Yes" /> Yes 
<input type="radio" name="opted[]" value="No" /> No

Above rows are cloned on every "Add friend" button click. 在每个“添加好友”按钮单击上克隆上面的行。 This works nicely for a text field, as I get values from all the rows on submit, but radio button doesn't work because it has the same name and clicking on it interferes with other cloned rows. 这对于文本字段很有效,因为我从提交中的所有行获取值,但是单选按钮不起作用,因为它具有相同的名称并且单击它会干扰其他克隆的行。

Any idea how to get around this issue? 知道如何解决这个问题吗?

Replace name="opted[]" with name="opted[123]" , where 123 is an unique identifier, so that each set of answets are grouped. name="opted[]"替换name="opted[]" name="opted[123]" ,其中123是唯一标识符,以便对每组答案进行分组。

Then, you can access the form elements through either of the following methods: 然后,您可以通过以下任一方法访问表单元素:

var method1 = document.getElementsByName("opted[123]");
var method2 = document.querySelectorAll("[name='opted[123]']");
//Both vanilla-JavaScript methods return a NodeList, consisting of all
//  elements with name="opted[123]"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM