问卷开始时,这是我的HTML:
<form id="questionnaire">
Question 1: Do you like dogs?<br>
<input type="radio" name="answer" value="yes">Yes<br>
<input type-"radio" name="answer" value="no">No<br>
<input type="submit" value="Next Question" />
</form>
到目前为止,这是我的Javascript:
$(document).ready(function(){
var q2 =
'Question 2: Do you like cats?
<input type="radio" name="answer" value="yes">Yes<br>
<input type-"radio" name="answer" value="no">No<br>
<input type="submit" value="Next Question" />'
$('#questionnaire').submit(function(){
$('#questionnaire').html(q2); // This puts the Question 2 HTML into the form
return false;
});
var q3 =
'Question 3: Do you like bunnies?
<input type="radio" name="answer" value="yes">Yes<br>
<input type-"radio" name="answer" value="no">No<br>
<input type="submit" value="Next Question" />'
});
我编写了什么样的函数,以便在问题2载入后,单击“下一个问题”按钮导致问题3?
最佳答案
Fiddle
Javascript:
var questions = ['Question 1: Do you like dogs?<br><input type="radio" name="answer" value="yes">Yes<br><input type-"radio" name="answer" value="no">No<br><input type="submit" value="Next Question" />', 'Question 2: Do you like cats?<input type="radio" name="answer" value="yes">Yes<br><input type-"radio" name="answer" value="no">No<br><input type="submit" value="Next Question" />','Question 3: Do you like bunnies?<input type="radio" name="answer" value="yes">Yes<br><input type-"radio" name="answer" value="no">No<br><input type="submit" value="Next Question" />'];
$('#questionnaire').html(questions[0]);
var currIdx = 0;
$(document).ready(function(){
$('#questionnaire').submit(function(){
currIdx++;
$('#questionnaire').html(questions[currIdx]); // This puts the Question 2 HTML into the form
return false;
});
});
HTML:
<form id="questionnaire">
</form>