2017-06-23 59 views
0

我正在開發一個測驗,我希望這個「檢查答案」按鈕被刪除,並在點擊任何選項時必須顯示答案。在這個JavaScript測驗中擺脫檢查答案按鈕?

任何人都可以請幫助我如何做到這一點?

我在這裏找到了重複, Creating a JavaScript Quiz that shows answer explanation automatically 但這不是我想要做的。因爲我是堆棧溢出的新手,所以我無法評論這個問題,所以打開這個新的。

這裏的jsfiddle鏈接:http://jsfiddle.net/natmit/7Du6N/

function processQuestion(choice) { 
     if (quiz[currentquestion]['choices'][choice] == quiz[currentquestion]['correct']) { 
      $('.choice').eq(choice).css({ 
       'background-color': '#84e47b' 
      }); 
      $('#explanation').html('<h5>Awesome! Right answer!</h5><br/>' + htmlEncode(quiz[currentquestion]['explanation'])); 
      score++; 
     } else { 
      $('.choice').eq(choice).css({ 
       'background-color': '#e0514e' 
      }); 
      $('#explanation').html('<h3>Oops! wrong answer</h3><br/>' + htmlEncode(quiz[currentquestion]['explanation'])); 
     } 
     currentquestion++; 
     $('#submitbutton').html('<h4>NEXT QUESTION &raquo;</h4>').on('click', function() { 
      if (currentquestion == quiz.length) { 
       endQuiz(); 
      } else { 
       $(this).text('CHECK ANSWER').css({ 
        'color': '#ffffff' 
       }).off('click'); 
       nextQuestion(); 

      } 
     }) 
    } 
+0

['$('#submitbutton').hide();'](http://api.jquery.com/hide/) – Pyromonk

+0

嗨,我是javascript新手,你能告訴我在哪裏打電話這和將選擇自動工作,並顯示結果,如果我只隱藏提交按鈕? –

+0

喂,拉朱。如果你可以請你解釋你正在努力達到的目標,或者展示一個你想做的事情的例子,這對幫助你很有幫助。 – Pyromonk

回答

0

嘗試在你的init()函數的末尾添加$('#submitbutton').hide();

改變你的按鈕在你setupButtons)的動作(功能有點像這樣:

 $('.choice').on('click', function() { 
     picked = $(this).attr('data-index'); 
     $('.choice').removeAttr('style').off('mouseout mouseover'); 
     $(this).css({ 
      'border-color': '#222', 
      'font-weight': 700, 
      'background-color': '#c1c1c1' 
     }); 
      processQuestion(picked); 
    }); 

在你processQuestion結束()函數$('#submitbutton').show();再次顯示按鈕,最後再次隱藏的按鈕您nextQuestion()函數的一端與$('#submitbutton').hide();

這裏工作小提琴:http://jsfiddle.net/6ft4sy7s/

好運:)

+0

非常感謝。你拯救了我的一天。我感謝你的時間和努力幫助我。 上帝保佑 –

+0

@RajuLOCM此腳本允許用戶在選擇答案後繼續點擊答案。 – WizardCoder

+0

如果您在第一個問題上單擊3個答案,當您單擊「下一個問題」時,它將打破腳本。 – WizardCoder

0

如果我理解正確的話,你需要做的就是從這個您setupButtons()函數中更改此代碼:

$('.choice').on('click', function() { 
picked = $(this).attr('data-index'); 
$('.choice').removeAttr('style').off('mouseout mouseover'); 
$(this).css({ 
    'border-color': '#222', 
    'font-weight': 700, 
    'background-color': '#c1c1c1' 
}); 
if (submt) { 
    submt = false; 
    $('#submitbutton').css({ 
    'color': '#000' 
    }).on('click', function() { 
    $('.choice').off('click'); 
    $(this).off('click'); 
    processQuestion(picked); 
    }); 
} 
}) 

到:

$('.choice').on('click', function() { 
    if (submt) { 
     submt = false; 
     picked = $(this).attr('data-index'); 
     $('.choice').removeAttr('style').off('mouseout mouseover'); 
     $(this).css({ 
      'border-color': '#222', 
      'font-weight': 700, 
      'background-color': '#c1c1c1' 
     }); 
     $('.choice').css({ 
      'cursor': 'default' 
     }); 

     processQuestion(picked); 
     $('#submitbutton').css({ 
      'color': '#000' 
     }); 
    } 
}) 

我在這裏做的所有事情都是以processQuestion()功能以外的$('#submitbutton')點擊事件,因此一旦選擇了選擇就會觸發它。

我還移動了$('.choice')單擊事件中的其餘jQuery代碼,並將其移入if (submt)條件語句中。這可以防止在選擇答案後激活答案選擇CSS。

我選擇了一個選項後,將cursor:default的CSS添加到選項中,這樣用戶很清楚他們不能再選擇它。

http://jsfiddle.net/7Du6N/326/

修訂FIDDLE:http://jsfiddle.net/7Du6N/328/ *過程中接聽按鈕隱藏

+0

非常感謝,但當我去你的小提琴「檢查答案按鈕」仍然存在。 下次我會更具體地解答我的問題。真的很感謝你的時間。 –

+0

@RajuLOCM我已經更新了小提琴。 – WizardCoder

+0

非常感謝,現在很棒 –