简体   繁体   English

在动态单选按钮中仅选择一个

[英]Select only one in dynamic radio button

It is possible to select only one radio button using a same name? 可以只选择一个使用相同名称的单选按钮吗? I want to select only one radio button in every question. 我只想在每个问题中选择一个单选按钮。

The problem is it only selects one radio button in all appended html elements. 问题在于它仅在所有附加的html元素中选择一个单选按钮。

DEFAULT HMTL: 默认HMTL:

//question 1
<input type="radio" name="fields[my_cb][]" value="1">
<input type="radio" name="fields[my_cb][]" value="2">

ADDED HTML ELEMENTS USING JAVASCRIPT APPEND: 使用JAVASCRIPT附录添加的HTML元素:

//question 2
<input type="radio" name="fields[my_cb][]" value="1">
<input type="radio" name="fields[my_cb][]" value="2">
<input type="radio" name="fields[my_cb][]" value="3">

You can only select one radio button in a group, and all the buttons with the same name are part of the same group. 您只能在一个组中选择一个单选按钮,并且所有具有相同名称的按钮都属于同一组。 You should use a different name for the answers to each question. 您应该为每个问题的答案使用不同的名称。 In your example, you can put the question number in the array index. 在您的示例中,您可以将问题编号放入数组索引中。

//question 1
<input type="radio" name="fields[my_cb][1]" value="1">
<input type="radio" name="fields[my_cb][1]" value="2">

//question 2
<input type="radio" name="fields[my_cb][2]" value="1">
<input type="radio" name="fields[my_cb][2]" value="2">
<input type="radio" name="fields[my_cb][2]" value="3">

To set a radio button in a button group to checked by default you use: checked="checked" 要将按钮组中的单选按钮设置为默认选中,请使用: checked="checked"

In order to have multiple button groups you need to name each group differently: 为了具有多个按钮组,您需要为每个组分别命名:

//question 1
<input type="radio" name="grp1" value="1" checked="checked">
<input type="radio" name="grp1" value="2">

//question 2
<input type="radio" name="grp2" value="1">
<input type="radio" name="grp2" value="2">
<input type="radio" name="grp2" value="3">

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

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