【问题标题】:how to check the value of an input correctly?如何正确检查输入的值?
【发布时间】:2013-06-12 02:00:28
【问题描述】:

我做了一个小测验 但我还有最后一个问题 单击下一步按钮时,它会检查是否未找到该值。 如果它是真的,它会检查客户选择的收音机是否等于配偶的正确答案 一个var correctAnswer 得到+1。 问题是在第一个问题上它的工作(检查客户是否真的从答案中选择了一些东西并检查它是否正确) 但是当提出下一个问题时,它不起作用。我试图弄明白,但没有得到解决... 还要检查 jsfiddle:http://jsfiddle.net/boazmier/ru8Qj/1/ 我试图将 checkClientChoice 函数放入 loadquestion 函数中,但效果不佳

var allQuestions = {
    question: {
        question0: {
            question: "Who is the first Prime Minister of Israel?",
            choices: ["David", "Rabin", "Peres", "Bibi"],
            correct: "David"
        },
        question1: {
            question: "What Is israel date of birth?",
            choices: ["1948", "1958", "1950", "1944"],
            correct: "1948"
        },
        question2: {
            question: "What is PhoneGap framework for?",
            choices: ["apache", "apps", "server", "client"],
            correct: "apps"
        }
    },

    correctAnswer: 0
};

var allquestion = allQuestions["question"];

var calcAnswers =allQuestions["correctAnswer"];

var qNum = 0;

var value;

//function that deploies the question and the choices to the section.
function loadquestions(qNum){
    $(".question").text(allquestion["question"+qNum]["question"]); //set the the question to the title
    for(var i=0;i<allquestion["question"+qNum]["choices"].length;){
        //loops throughout the li's
        var cH =(allquestion["question"+qNum]["choices"][i]); // a var that holds text of choice in poistion i
        $("label[for=li"+i+"]").text(cH); //define for every label its text
        $("#li"+i).attr("value",cH); //define for every input radio its value which is equals to label which is equals to var cH
        i++;//count i with +1
    }
}

//function that fires when clicked the "next" button and when trigered hides the current question and presents the next one

function checkClientChoice(){
 $("#next").click(function(){
     value = $("input[type='radio']:checked").val();

     if(value == undefined){
        alert("Please choose a valid answer");
    }
    else {
        if(allquestion["question0"]["correct"] == value){
            alert("correct");
            calcAnswers++;
            alert(calcAnswers);
            qNum++;
            loadquestions(qNum);
        }
         else{
            qNum++;
            loadquestions(qNum);
        }
    }
});
};

$(document).ready(function(){
    loadquestions(qNum);
    checkClientChoice();
});

【问题讨论】:

  • 旁注:你真的应该使用数组键 0,1,2,... 而不是 question0,question1,etc..
  • 简洁而传统。

标签: javascript jquery input radio-button


【解决方案1】:

好的,我们得到了什么:

if(allquestion["question0"]["correct"] == value)

您总是检查第一个问题的答案。您应该将其更改为

if(allquestion["question" + qNum]["correct"] == value)

或以其他方式在函数checkClientChoice 中跟踪问题编号。

【讨论】:

  • 哦,我现在明白了 :) 我想我需要用新的眼光来检查我的代码... thnak mate。
猜你喜欢
  • 2016-04-26
  • 2022-11-11
  • 1970-01-01
  • 2017-12-14
  • 1970-01-01
  • 2022-01-19
  • 1970-01-01
  • 2017-07-24
  • 2021-02-16
相关资源
最近更新 更多