Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/72.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 - Fatal编程技术网

Javascript 如何检查在单击“提交”按钮时选择了哪个动态创建的单选按钮?

Javascript 如何检查在单击“提交”按钮时选择了哪个动态创建的单选按钮?,javascript,jquery,html,Javascript,Jquery,Html,我知道这个问题以前被问过很多次,但我还没有找到一个适合我的具体案例的解决方案 所以我有一个单选按钮,我最初是静态创建的。下面给出了HTML代码 我尝试了上面的代码,但它似乎只适用于第一个静态创建的单选按钮。如何使所有单选按钮(静态和动态生成)都能使用相同的功能 只要您的表单只处理一组单选按钮,您就可以这样做,只需将formId更改为包装收音机的表单所使用的任何类/id即可 $('input[name=radioName]:选中','formId').val() 这就是你能做到的。参考工作演示。

我知道这个问题以前被问过很多次,但我还没有找到一个适合我的具体案例的解决方案

所以我有一个单选按钮,我最初是静态创建的。下面给出了HTML代码

我尝试了上面的代码,但它似乎只适用于第一个静态创建的单选按钮。如何使所有单选按钮(静态和动态生成)都能使用相同的功能


只要您的表单只处理一组单选按钮,您就可以这样做,只需将formId更改为包装收音机的表单所使用的任何类/id即可

$('input[name=radioName]:选中','formId').val()


这就是你能做到的。参考工作演示。

#动态克雷迪奥{
左边距:100px;
}
#点击我{
左边距:100px;
边缘顶部:10px;
}
//这里我们创建动态单选按钮。
var create_dynamic_radio=$(“我的单选按钮”)
$(“#dynamicradio”).append(创建#dynamicradio);
//这里我们编写了click函数的代码。
$(“#单击我”)。单击(函数(){
var isradiochecked=$(“#myradio”).prop('checked');//检查是否选中了动态单选按钮。
如果(isradiochecked==true)
{
警报(“选中动态单选按钮”);
}
其他的
{
警报(“未选中动态单选按钮”);
}
});

“试试这个”对任何人都没有帮助。发生了什么变化?为什么工作?@AnuradhS是否需要通过变量创建动态单选按钮?另外,是否需要创建新的动态单选按钮作为单独div标记的一部分?如果需要,可以不使用变量来创建它。
    <body>
        <div class = 'container'>
            <div class = 'row'>
                <div class = 'col-md-8 col-md-offset-2 well'>
                    <div class = 'col-md-4'>
                        <h1>Title Placeholder</h1>
                        <p>My vote goes to</p>
                        <div class = 'radio'>
                            <label>
                                <input type = 'radio' value = '1' name = 'options' class = 'radio-options'/>
                                <p id = 'radio1'>The value of 1</p>
                            </label>
                        </div>
                        <button type = 'submit' class = 'btn btn-primary submit-butt'>Submit</button>
                    </div>
                </div>
            </div>
        </div>   

    </body>
function createOptions(length, options){
    $('#radio1').html(options[0]);
    for (var i = 1; i < length; i++){
        $('.radio').append('<br>');
        $('.radio').append('<label><input type = radio value = 1 name = options class = radio-options/><p>'+options[i]+'</p></label>');
    }
    $('.radio').append('<br>');
    $('.radio').append('<label><input type = radio value = 1 name = options class = radio-options/><p>Custom</p></label>')
}
$('.submit-butt').on('click', function(){
                   if($('.radio-options').is(':checked')){
                       console.log('I have my value');
                   } 
                });
$('.submit-butt').on('click', function(){
   var selectedRadioValue = $('input[name=radioName]:checked','#formId').val()
   console.log(selectedRadioValue)
});