2016-02-22 46 views
0

我有一個測驗佈局,從數據庫中獲取問題和答案。隱藏點擊並顯示另一個直到最後

我目前有一個下一個按鈕來通過這些問題,但我想這樣做,所以當選擇一個答案時,它會轉到下一個問題

每個問題/答案集都有這個div,id是問題的ID。

<div class="question container center" id="1" style="display: none;"> 
    <h1>Question</h1> 
    Answer 
    Answer 
    Answer 
    Answer 
</div> 

答案按鈕看起來是這樣的:

<a class="waves-effect waves-light btn-large blue-grey lighten-2 btn-width mcqtest">Answer</a> 

使用當前Jquery的IM是這樣的:

$('#2, #3, #4, #5, #6, #7, #8, #9, #10, #11, #12, #13, #14, #15, #16, #17, #18, #19, #20, #21, #22, #23, #24, #25 ').hide(); 
$('#next').click(function(){ 
    //code 
    var curr = $(".question:visible"); 
    var next = curr.next(".question"); 
    next.show(); 
    curr.hide(); 
    if (!next.next(".question").length) { 
     $("button").attr('disabled', 'disabled'); 
     $("button").text("End of Test"); 
    } 
}); 
+0

重要的是,'id's不應該以數字開頭。 – RRK

+0

答案的元素類型是什麼? –

+0

答案只是風格的標籤 – JohnyChew

回答

3

使用prop()而不是attr(),我做了一些其他的變化。所有的

$('.question').slice(1).hide(); 
$('#next, a.mcqtest').click(function(){ 
    //code 
    var curr = $(".question:visible"); 
    var next = curr.next(".question"); 
    next.show(); 
    curr.hide(); 
    if (!next.next(".question").length) { 
     $("button").prop('disabled', true); 
     $("button").text("End of Test"); 
    } 
}); 
2

首先,如果可能的變化ID的(#2,#3等),以一個類。 下一步 - 從$('#next')提取您的匿名函數,點擊,然後將點擊監聽器添加到每個答案鏈接,這樣每次任何人選擇答案時都會觸發您的「下一個問題」函數。

例如(代碼不進行身份的改變):

function nextQuestion(){ 
    var curr = $(".question:visible"); 
    var next = curr.next(".question"); 
    next.show(); 
    curr.hide(); 
    if (!next.next(".question").length) { 
     $("button").attr('disabled', 'disabled'); 
     $("button").text("End of Test"); 
    } 
} 

$('#2, #3, #4, #5, #6, #7, #8, #9, #10, #11, #12, #13, #14, #15, #16, #17, #18, #19, #20, #21, #22, #23, #24, #25 ').hide(); 
$('#next').click(nextQuestion); 
$('a.mcqtest').click(nextQuestion); 
2

添加這個腳本,當你要求它必須工作。

$('a.mcqtest').on('click',function(e){ 
    e.preventDefault(); 
    $('#next').trigger('click'); 
}); 
相關問題