如何正确检查输入的值
how to check the value of an input correctly?
我做了一个小测验但我还有最后一个问题当单击next按钮时,它会检查该值是否未找到。如果为真,它会检查客户端选择的无线电是否等于配偶的正确答案a 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();
});
好的,这是我们得到的:
if(allquestion["question0"]["correct"] == value)
你总是检查第一个问题的答案。您应该将其更改为
if(allquestion["question" + qNum]["correct"] == value)
或以其他方式跟踪函数checkClientChoice
中的问题num
相关文章:
- 为什么不't Javascript对我的输入值进行了一些重新检查
- 使用javascript检查多个输入值,并在1次检查中标记多个输入框
- 使用onkeyup JS事件检查输入的值是否唯一
- ajax在输入等于null时进行检查
- 我确实有一个表单,如果用户输入了输入,它应该检查否定或空的输入框,并抛出一条警告消息
- jQuery从输入中获取值并检查它是否'It’是的.如果是这样,它应该公布有多少是正确的
- 如何使用JQuery检查输入(电子邮件)字段是否包含特殊字符
- 检查输入是否未更改
- 提交前表单输入检查
- HTML - 使用停止PHP脚本的javascript进行输入检查
- AngularJS输入检查/取消检查有2个功能
- 输入检查用户输入的值介于 0-9 之间 Javascript
- 字符串验证的输入检查
- 创建列表输入检查数组,用于发布REST API
- 密码&提示输入检查
- jQuery空输入检查功能没有按预期工作
- 引导表单输入:防止提交,但允许输入检查
- 根据多个文件输入检查现有文件名
- Chrome扩展文本框输入检查
- 用于输入检查的 JavaScript 函数不起作用