使用JavaScript或jQuery检查单选按钮选择是否与数组中的项匹配

使用JavaScript或jQuery检查单选按钮选择是否与数组中的项匹配,javascript,jquery,radio-button,dynamically-generated-co,Javascript,Jquery,Radio Button,Dynamically Generated Co,在这一点上,我只是测试,看看我是否可以比较单选按钮选择与数组中的正确答案。见以下评论: var allQuestions = [{ "question": "Who was Luke's wingman in the battle at Hoth?", "choices": ["Dak", "Biggs", "Wedge", "fx-7"], "correctAnswer": 0 }]; function answerFwd() {

在这一点上,我只是测试,看看我是否可以比较单选按钮选择与数组中的正确答案。见以下评论:

var allQuestions = [{
        "question": "Who was Luke's wingman in the battle at Hoth?",
        "choices": ["Dak", "Biggs", "Wedge", "fx-7"],
        "correctAnswer": 0
    }];


function answerFwd() {
    var answerOutput = " ";
    var itemAnswers = allQuestions;
    var answer = 0;
    var playerTally = 0;
    var playerFeedback = "";
    var playerMessage = document.getElementById("playerMessage");


    // Is this the correct way to store the radio button which is selected?
    var radioValue = $("input[type='radio'].radioButtons").is(':checked');

    var right = false;

    if (currentAnswer <= itemAnswers.length) {
        currentAnswer++;
    }

    createRadioButtonFromArray(itemAnswers[currentQuestion].choices);

    //Then compare value in array with radioValue value
    if(radioValue == itemAnswers.correctAnswer){
          right = true;
    }           

    if(right) {
        alert("You answered correctly");
    } else {
        alert("Wrong answer");
    }
它是这样呈现的:

<div id="responses">
<input type="radio" name="choices" class="radioButtons" value="0" id="choice0">
<div id="c0" class="choiceText">The Avenger</div>
<input type="radio" name="choices" class="radioButtons" value="1" id="choice1">
<div id="c1" class="choiceText">Devastator </div>
<input type="radio" name="choices" class="radioButtons" value="2" id="choice2">
<div id="c2" class="choiceText">Conquest</div>
<input type="radio" name="choices" class="radioButtons" value="3" id="choice3">
<div id="c3" class="choiceText">The Executor</div>

复仇者
毁灭者
征服
执法者

这是存储所选单选按钮的正确方法吗

var radioValue = $("input[type='radio'].radioButtons").is(':checked');
不,那将是:

var radioValue = $("input[type='radio'].radioButtons:checked").val();

呃,达克是卢克的枪手,不是他的僚机。太棒了!这是真的。“
复仇者”
“你要找的元素就在那里。@T.J Crowder我有点困惑?!我想我仍然没有正确地匹配单选按钮的值和数组中的值。有什么想法吗?为了提供一些背景知识,我正在做一个小测验……如果现在还不清楚的话……)@安东尼奥·奥尔蒂斯:如果你能显示单选按钮,那会有很大帮助。
var radioValue = $("input[type='radio'].radioButtons:checked").val();