【问题标题】:Getting Value from Radio with Jquery使用 Jquery 从 Radio 中获取价值
【发布时间】:2013-10-29 08:27:29
【问题描述】:

我正在寻找一种从单选按钮中获取价值的方法。我这里有html。

<div id="main_">
    <div class="facts_div">
        <span class="question"></span>
        <ul></ul>
    </div>
    <div id = "next_button">
    <form>
        <input id="x" type="button" class="myBtn" value="Press Me">
    </form>
    </div>
</div>
</div>

这是 Jquery 代码。

$(document).ready (function () {

//

var answers = 
[["Fee","Fi","Fo"], 
["La","Dee","Da"]],
questions = 
["Fee-ing?",
"La-ing?"],
corAns = ["Fee", "La"];

var counter = 0;
var correctAnswers = 0;

var $facts = $('#main_ .facts_div'),
$question = $facts.find('.question'),
$ul = $facts.find('ul'),
$btn = $('.myBtn');


$btn.on('click', function() {
    if (counter < questions.length) {
        $question.text(questions[counter]);

        var ansstring = $.map(answers[counter], function(value) {
            return '<li><input type="radio" name="ans" value="0"/>'
            + value + '</li>'}).join('');
        $ul.html(ansstring);
        alert($('input[name="ans"]:checked').val())
        //if ($('input[name=ans]:checked').val() == corAns[counter]) {
            //correctAnswers++;
        //}
    }
    else {
        $facts.text('You are done with the quiz ' + correctAnswers);
        $(this).hide()
    }
    counter++;
});



//    
});

如您所知,它一直返回未定义,我不太确定自己做错了什么。

顺便说一句,价值在于衡量我正在做的测验中这是正确还是错误的答案。所以,最终我需要在 if 语句中使用array[counter]。但是,我把它拆开来测试和调整并弄清楚如何获得价值。

【问题讨论】:

  • 页面加载是否有任何检查?
  • 我不知道该怎么做,所以没有。
  • 因此,在您的代码中,您会在页面加载时提醒选中的单选按钮值,但由于在页面加载时未检查任何内容,因此您收到未定义
  • 那么,我应该嵌套另一个处理单选按钮的onclick,然后将值添加到correctAnswers吗?
  • 好吧,我不知道我是否在这里不清楚,但也许我应该只发布整个测验,而不是这个 sn-p。我想动态地循环问题,然后在选择单选按钮选项时,返回要测试的值。如果它实际上是正确答案,我希望将其添加到正确答案中。

标签: jquery jquery-selectors radio-button


【解决方案1】:

我相信你让这件事变得比需要的复杂得多。您只需要一组正确答案,然后单击按钮即可循环并检查答案(您将在 cmets 中看到逐个播放的播放):

$btn.on('click', function() {
    //Your answers array should never be longer than your questions length, so that check is a bit useless, scrap it
    //Lets first gather all your answers into an array, the .map function is built for this:

    var currentAnswers = $('input[name="ans"]:checked').map(function() {
        return this.value;
    }).get();

    //Now that we have all the checked radio buttons from the quiz, loop and compare to your answers array, if they match, do something
    //You have a 2D array of answers, then one of correct answers, you only need 1 array, and that should contain the correct responses, so eliminate the 2D array
    //Your correct answers should be: corAns = ["Fee", "La"];

    var correct = 0; //current number of correct answers
    for (var i = 0; i < currentAnswers.length; i++) {
        if (currentAnswers[i] == corAns[i]) {
            correct++; //Increment correct variable
        }
    }
});

当然,您可能需要检查以确保用户已回答所有问题,否则最终数组可能会超出范围并导致错误。

【讨论】:

  • 嘿,非常感谢。如果我在我的任何一个 cmets 中听起来很尖刻,我很抱歉,那不是我的本意。我已经很久没有这样做了。有时,它会变得有点令人沮丧(不过以一种好的方式)。这种方式要优雅得多。
  • 发展就是这样,有很多不同的方式来做事。坚持下去,继续提问!乐意效劳! -@JerryWalker
  • 我还有一个问题...如果整个测验只有一个数组,那么我将如何动态添加所有答案?实际上,显示的唯一答案将是正确的答案。
猜你喜欢
  • 2010-12-13
  • 1970-01-01
  • 1970-01-01
  • 2023-03-03
  • 2011-11-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-24
相关资源
最近更新 更多