我一直在開發一個jQuery測驗,我已經能夠將每個答案的值加在一起,我想添加一個函數來測驗,將允許特定值添加到一組每個問題之後的變量已被回答。增量分數,jQuery測驗
我已經包含了一個jsFiddle,在任何值被註冊之前,您可以看到每個問題何時單擊其第三個問題,並且如果添加了第四個問題,則增加的值會被添加三次。
的jsfiddle:http://jsfiddle.net/jamcrowe/ta7LZ/1/
// Answers to each question add these values to finalResult
var value = {
'question0' : { one: 1, two: 2, three: 3, four: 4 },
'question1' : { one: 1, two: 2, three: 3, four: 4 },
'question2' : { one: 1, two: 2, three: 3, four: 4 }
};
// The next question to present after each response
var END = null;
var nextQuestion = {
'question0' : { one: 'question1', two: 'question1', three: 'question1', four: 'question1', },
'question1' : { one: 'question2', two: 'question2', three: 'question2', four: 'question2', },
'question2' : { one: END, two: END, three: END, four: END, },
};
// Show just the first question
$('.ques').hide();
$('#question0').fadeIn();
var outcome = 0;
$('.option').click(function(){
increment();
var answer = $(this).attr('value');
var question = $(this).attr('name');
outcome += value[question][answer];
$('#' + question).delay(500).fadeOut(function(){
var questionNext = nextQuestion[question][answer];
if (questionNext == END){
var finalResult = 'result ' + outcome;
alert("Values added together : " + finalResult);
}
else {
$('#' + questionNext).delay(2000).fadeIn(1000);
}
});
});
var online = 0;
var creative = 0;
var technical = 0;
var analyst = 0;
var managerial = 0;
function increment() {
$('#q1a').click(function(){
creative +=5;
online ++;
managerial ++;
});
$('#q2a').click(function(){
creative +=5;
online ++;
managerial ++;
});
$('#q3a').click(function(){
creative +=5;
online ++;
managerial ++;
});
$('#q4a').click(function(){
creative +=5;
online ++;
managerial ++;
});
alert(creative);
}
http://jsfiddle.net/jamcrowe/ta7LZ/1/ –