如何确定在Javascript中选择了哪个单选按钮

如何确定在Javascript中选择了哪个单选按钮,javascript,radio-button,selected,Javascript,Radio Button,Selected,嘿,伙计们,我正在试图找出如何确定哪个单选按钮是只使用javascript选择的。。。我知道使用一些jquery会更容易,但我现在更喜欢javascript。。我知道我可以通过名称(getElementsByName)获取元素的值,并从中确定它。但不知怎么的,它不起作用了……这是我的JS小提琴 下面是mycode示例。。。谢谢 var questions = { allQuestions : [

嘿,伙计们,我正在试图找出如何确定哪个单选按钮是只使用javascript选择的。。。我知道使用一些jquery会更容易,但我现在更喜欢javascript。。我知道我可以通过名称(getElementsByName)获取元素的值,并从中确定它。但不知怎么的,它不起作用了……这是我的JS小提琴 下面是mycode示例。。。谢谢

    var questions = {

                        allQuestions : [


                    {
                        topQuestion:["Click on which producer produced Justins Timberlake 4th Album?", "What was the first disney movie Justin Timberlake firsr scored?", "What famous celebrity did Justin Timberlake dated ?", "Star Wars"],
                    },    
                    {
                        question: "Select which movie did Justin Timberlake film score in 2008?", 
                        choices:["Shark Tank", "The Incredibles", "Finding Memo", "Star Wars"],
                        correctAnswer:3
                    },
                    {

                        question:"What city was Justin Timberlake born in?",
                        choices: ["Chicago", "Detroit", "Tenessee", "New York"],
                        correctAnswer:3
                    },
                     {

                        question:"At the age of 11, what famous show did Justin Timberlake appeared on?",
                        choices: ["American Idol", "Family Fued", "Star Search", "The Voice"],
                        correctAnswer:3
                    },
                     {

                        question:"What hit single did Justin Timberlake perform at the MTV Awards in 2002",
                        choices: ["Sexy Love", "Cry Me A River", "Like I Love You", "What Comes Around"],
                        correctAnswer:3
                    },
                     {

                        question:"What boy band was Justin Timberlake apart of?",
                        choices: ["One Direction", "Black Street Boys", "98 Degrees", "NSync"],
                        correctAnswer:4
                    }    
                        ]
                    };

                    var newQues = Object.create(questions);


                    for (var i = 0; i < 4; i++){


                    container = document.getElementById("container");
                    list = document.getElementById("list");
                     var li = document.createElement("input");     
                     li.type = 'radio';
                     li.name= 'radio_group';    
                     li.id = 'id1'; 
                     li.value = newQues.allQuestions[1].correctAnswer;                        

                     document.body.appendChild(li); 
                     el = document.createElement("div");
                     text = document.createTextNode(newQues.allQuestions[1].choices[i])
                     list.appendChild(el); 
                     el.appendChild(li);
                     el.appendChild(text);                        

                    }


             var radios = document.getElementsByName("radio_group");
              for (var i = 0; i < radios.length; i++) {
               if (radios[i].checked) {                       
                 alert(radios[i].value)                          
                   }
               };
var问题={
所有问题:[
{
主题问题:[“点击哪个制作人制作了贾斯汀·汀布莱克的第四张专辑?”,“贾斯汀·汀布莱克的第一部迪斯尼电影是什么?”,“贾斯汀·汀布莱克和哪个名人约会了?”,“星球大战”],
},    
{
问题:“选择贾斯汀·汀布莱克在2008年拍摄的电影?”,
选择:[“鲨鱼坦克”、“不可思议的人”、“寻找备忘录”、“星球大战”],
正确答案:3
},
{
问题:“贾斯汀·汀布莱克出生在哪个城市?”,
选择:[“芝加哥”、“底特律”、“特纳西”、“纽约”],
正确答案:3
},
{
问题:“贾斯汀·汀布莱克11岁时参加了什么著名的节目?”,
选择:[“美国偶像”、“家庭主妇”、“明星搜索”、“声音”],
正确答案:3
},
{
问题:“贾斯汀·汀布莱克在2002年MTV颁奖典礼上表演了什么热门单曲”,
选择:[“性感的爱”,“哭我一条河”,“喜欢我爱你”,“会发生什么”],
正确答案:3
},
{
问题:“贾斯汀·汀布莱克是哪个男孩乐队的成员?”,
选择:[“单向”、“黑人街头男孩”、“98度”、“NSync”],
正确答案:4
}    
]
};
var newQues=Object.create(问题);
对于(变量i=0;i<4;i++){
容器=document.getElementById(“容器”);
list=document.getElementById(“list”);
var li=document.createElement(“输入”);
li.type='radio';
li.name='radio_group';
li.id='id1';
li.value=newQues.allQuestions[1]。正确答案;
文件.正文.附件(li);
el=document.createElement(“div”);
text=document.createTextNode(newQues.allQuestions[1]。choices[i])
列表。附加子对象(el);
el.儿童(李);
el.附件(文本);
}
var radios=document.getElementsByName(“radio_组”);
对于(变量i=0;i
演示:

您的单选按钮没有eventListener,我添加了一个它现在可以工作了

radios[i].addEventListener('change', function() {
    alert(this.value);
});

el=document.createElement(“el”)
?@RobG-el元素保存输入元素,即(li)…然后将其附加到文档正文中。->>对不起,我太迟钝了。createElement的参数应该是标记名,“el”在HTML文档中不是有效的标记名。没问题@RobG,好的,我只是将“el”更改为有效的标记名“div”。。但肯定有什么地方出错了。