Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从不同的单选按钮组中获取单选按钮标签并附加到分区_Javascript_Jquery_Html_Append_Radio - Fatal编程技术网

Javascript 从不同的单选按钮组中获取单选按钮标签并附加到分区

Javascript 从不同的单选按钮组中获取单选按钮标签并附加到分区,javascript,jquery,html,append,radio,Javascript,Jquery,Html,Append,Radio,有3个单选按钮组供用户选择。 每当用户选中要显示的单选按钮标签时,分区“#targetOption”将获得。下面是我的代码,这是能够得到第一个选中单选按钮 例如,如果用户单击第一个单选按钮组,#targetOption将显示A。然后,如果用户单击第二个单选按钮组,#targetOption将显示B <div id="options"> <div class="form-group required"> <div class="radio"&

有3个单选按钮组供用户选择。 每当用户选中要显示的单选按钮标签时,分区“#targetOption”将获得。下面是我的代码,这是能够得到第一个选中单选按钮

例如,如果用户单击第一个单选按钮组,#targetOption将显示A。然后,如果用户单击第二个单选按钮组,#targetOption将显示B

<div id="options">
    <div class="form-group required"> 
        <div class="radio"> 
            <label> <input type="radio" value="a">
            A   
            </label> 
        </div>
        <div class="radio"> 
            <label> <input type="radio" value="b">
            B   
            </label> 
        </div>
        <div class="radio"> 
            <label> <input type="radio" value="c">
            C   
            </label> 
        </div>
    </div>

    <div class="form-group required"> 
        <div class="radio"> 
            <label> <input type="radio" value="d">
            D   
            </label> 
        </div>
        <div class="radio"> 
            <label> <input type="radio" value="e">
            E  
            </label> 
        </div>
        <div class="radio"> 
            <label> <input type="radio" value="f">
            F   
            </label> 
        </div>
    </div>

    <div class="form-group required"> 
        <div class="radio"> 
            <label> <input type="radio" value="g">
            G   
            </label> 
        </div>
        <div class="radio"> 
            <label> <input type="radio" value="h">
            H   
            </label> 
        </div>
        <div class="radio"> 
            <label> <input type="radio" value="i">
            I   
            </label> 
        </div>
    </div>
  </div>  
    <div id="targetID"></div>

        $('#options').click(function() {
                        $('#targetID').html('');

                        $("input[type='radio']:checked").each(function() {
                          var optiontext = $(this).parent().text();
                          console.log(optiontext);
                         $('#targetID').html(optiontext);
                        });


                      });

A.
B
C
D
E
F
G
H
我
$(“#选项”)。单击(函数(){
$('#targetID').html('');
$(“输入[type='radio']:选中”)。每个(函数(){
var optiontext=$(this.parent().text();
控制台日志(optiontext);
$('#targetID').html(optiontext);
});
});
$('#targetID').html(optiontext)
#targetID
设置为循环中的当前无线电值,而不是将其附加到
#targetID

使用
$('targetID').html($('targetID').html()+optiontext)取而代之


或者您可以创建一个数组来存储选中的值,并在以后更新
#targetID

谢谢,它可以工作!我如何将此标记为解决方案?我是新来的