我是javascript的新手,我的问题对某些人来说似乎很容易,但我坚持一次正确答案的分数增加。
例子:对于我的第一个问题,答案是巴黎。如果我在文本框中写巴黎得分应增加1,对于第二个问题,答案是法国,如果我在文本框中写法国分数应该增加1.这应该只做一次,即如果我删除巴黎和写巴黎分数不应再增加。
<div id="score">score : 0</div>
<input type="text" id="question"/>
<input type="text" id="question1"/>
var score = 0;
$("#question").blur(function() {
if ($(this).val() == 'Paris')
score++;
});
$("#question1").blur(function() {
if ($(this).val() == 'France')
score++;
});
答案 0 :(得分:1)
如果问题已得到解答,您可以在文本框中添加属性:
<input type="text" id="question" data-answered="true"/>
答案 1 :(得分:1)
您可以使用以下评分模型跟踪您回答的问题:
var questions = [
{
fieldId : "#question",
correctAnswer : "Paris",
enteredAnswer : null,
},
{
fieldId : "#question1",
correctAnswer : "France",
enteredAnswer : null,
},
];
然后,你可以这样做:
<script type="text/javascript">
//we need to wait until the document is loaded, or our elements will not be available yet
$(document).ready(function(){
//for each of the question input fields, when they blur(), we do the following:
$(".questionclass").blur(function()
{
//we start counting the score
var countedScore = 0;
//we iterate through all the questions in our question model
for(var i =0; i < questions.length; i++)
{
//we fetch the current question
var activeQuestion = questions[i];
//if there is no answer entered for that question
if(activeQuestion.enteredAnswer == null)
{
//we find the field that should contain the answer to this question
var questionField = $(activeQuestion.fieldId);
//we then check if the user actualy inputted a value in that field.
if(questionField.val() != "")
{
//if he did, we update the entered answer in our model
activeQuestion.enteredAnswer = questionField.val();
}
}
//if this question has the right answer
if(activeQuestion.enteredAnswer == activeQuestion.correctAnswer)
{
//we increment the counted score
countedScore++;
}
}
alert("your score is " + countedScore);
});
});
</script>
<input type="text" name="something" id="question" class="questionclass"></input>
<input type="text" name="something" id="question1" class="questionclass"></input>
答案 2 :(得分:0)
questionScored = false;
question1Scored = false;
$("#question").blur(function() {
if ($(this).val() == 'Paris' && questionScored == false){
questionScored = true;
score++;
}
});
$("#question1").blur(function() {
if ($(this).val() == 'France' && question1Scored == false){
question1Scored = true;
score++;
}
});
答案 3 :(得分:0)
检查是否已保存答案。
var score, ans1, ans2;
score = ans1 = ans2 = 0;
$("#question").blur(function () {
if ($(this).val() == 'Paris') {
if (!ans1) {
ans1=1;
score++;
}
}
$("#score").text("score : " + score);
});
$("#question1").blur(function () {
if ($(this).val() == 'France') {
if (!ans2) {
ans2=1;
score++;
}
}
$("#score").text("score : " + score);
});
答案 4 :(得分:0)
只需使用标志变量进行跟踪。
<div id="score">score : 0</div>
<input type="text" id="question"/>
<input type="text" id="question1"/>
var score = 0;
q = true
q1 = true
$("#question").blur(function() {
if ($(this).val() == 'Paris' && q==true){
score++;
q = false;
}
});
$("#question1").blur(function() {
if ($(this).val() == 'France' && q1==true){
score++;
q1 = false;
}
});
答案 5 :(得分:0)
试试这个
<div id="score">score : 0</div>
<input type="text" id="question"/>
<input type="text" id="question1"/>
<script>
function valid()
{
var score=0;
if($("#question").val()=="Paris")
score++;
if($("#question1").val()=="France")
score++;
$("#score").text("score : "+score)
}
$("#question1,#question").blur(function(){
valid()
});
答案 6 :(得分:0)
我会将得分移动到一个函数中,这样如果他们将正确的答案更改回不正确的答案,那么它就不会被评分:
function checkResults() {
var score = 0;
if ($('#question').val() == 'Paris') {
score++;
}
if ($('#question1').val() == 'France') {
score++;
}
$('#score').text('score: ' + score);
}
$('input').blur(checkResults);