2012-05-23 79 views
0

我正在做一個練習,用戶必須從列表中檢查正確答案,然後按提交以查看他/她做得如何。但是,我的提交按鈕似乎沒有工作。點擊時沒有任何反應。下面是該腳本:循環選中複選框,然後顯示結果

<script> 
//Returns how many correct answers were checked 
$('#submitButton').click(function(){ 
    var arrayScore = [];   
    var tempScore = 0; 
    $('.confirmContainer').remove(); 
    $('.toggleConfirmList:checked').each(function(){ 
     arrayScore.push($(this).val()); 
    }); 
    for(var i=0; i<arrayScore.length; i++) 
    { 
     tempScore = arrayScore[i] + tempScore; 

    } 

    $('feedback').show(); 
    $("#scoreArea").val(tempScore);  

}); 
</script> 

基本上我想禁用輸入容器中,然後計算用戶得分後顯示的反饋。

下面是腳本使用HTML代碼:

<ol class="toggleConfirmList" start="1"> 
         <!-- Start of toggleConfirm question --> 
         <li class="toggleConfirm"> 
          <p class="question">What is your citizenship? 
          </p> 
          <div class="toggleInputContainer"> 
          <input type="checkbox" value="1"> 
          </div> 
         </li> 
         <li class="toggleConfirm"> 
          <p class="question">What is the purpose of your trip</p> 
          <div class="toggleInputContainer"> 
          <input type="checkbox" value="1"> 
          </div> 
         </li> 
         <li class="toggleConfirm"> 
          <p class="question">How long will you be staying in Canada?</p> 
          <div class="toggleInputContainer"> 
          <input type="checkbox" value="0"> 
          </div> 
         </li> 
         <li class="toggleConfirm"> 
          <p class="question">Where will you be staying?</p> 
          <div class="toggleInputContainer"> 
          <input type="checkbox" value="0"> 
          </div> 
         </li> 
         <li class="toggleConfirm"> 
          <p class="question">What is your occupation?</p> 
          <div class="toggleInputContainer"> 
          <input type="checkbox" value="0"> 
          </div> 
         </li> 
         <li class="toggleConfirm"> 
          <p class="question">Do you have any family in Canada?</p> 
          <div class="toggleInputContainer"> 
          <input type="checkbox" value="1"> 
          </div> 
         </li> 
         <li class="toggleConfirm"> 
          <p class="question">How will you support yourself in Canada?</p> 
          <div class="toggleInputContainer"> 
          <input type="checkbox" value="0"> 
          </div> 
         </li> 
         <li class="toggleConfirm"> 
          <p class="question">Do you intend to work or study while you are in Canada?</p> 
          <div class="toggleInputContainer"> 
          <input type="checkbox" value="0"> 
          </div> 
         </li> 
         <li class="toggleConfirm"> 
          <p class="question">Have you ever been convicted of a criminal offence?</p> 
          <div class="toggleInputContainer"> 
          <input type="checkbox" value="0"> 
          </div> 
         </li> 
         <li class="toggleConfirm"> 
          <p class="question">Do you suffer from any medical conditions?</p> 
          <div class="toggleInputContainer"> 
          <input type="checkbox" value="0"> 
          </div> 
         </li> 
         <li class="toggleConfirm"> 
          <p class="question">Have you ever been refused a visa to Canada?</p> 
          <div class="toggleInputContainer"> 
          <input type="checkbox" value="0"> 
          </div> 
         </li> 
         <li class="toggleConfirm"> 
          <p class="question">In which country do you reside permanently?</p> 
          <div class="toggleInputContainer"> 
          <input type="checkbox" value="1"> 
          </div> 
         </li>           
        </ol> 

         <div class="confirmContainer"> 
      <input type="button" id="submitButton" value="Submit"> 
      </div> 
         <div class="feedback"> 
            <strong>Answer:</strong>In the exercise you checked <span id="scoreArea">0</span> correct questions 

         </div> 
+0

'$( '反饋')顯示();'。應該是'$('。feedback')。show();' – jbabey

+0

檢查javascript錯誤 – Imdad

回答

1

設定值,跨度元素,你應該使用text()方法而不是val()

//Returns how many correct answers were checked 
$('#submitButton').click(function(){ 
    var arrayScore = [];   
    var tempScore = 0; 
    $('.confirmContainer').remove(); 
    $(':checkbox:checked').each(function(){ // for selecting checkboxes you can use ":checkbox" selector 
     arrayScore.push(parseInt($(this).val())); // parseInt() the values 
    }); 
    for(var i=0; i<arrayScore.length; i++) 
    { 
     tempScore = arrayScore[i] + tempScore; 

    } 

    $('.feedback').show(); // class selectors should have '.' 
    $("#scoreArea").text(tempScore); // use text() instead of val()  

}); 

http://jsfiddle.net/EWgzq/2/

+0

Perfect ,非常感謝,我知道這些錯誤看起來很可憐​​,但它是我的第一個coop工作時間,我從來沒有在我的生活中做過網絡開發:( – user1410668

+0

@ user1410668歡迎您,不,這些都是常見的錯誤,祝您好運 – undefined

+0

May I也提出了一些調整:首先嚐試使用'.toggleConfirmList輸入:checked'選擇器,因爲它會更快更清晰,也可能根本不需要將值推入數組中(取決於您的總體需求,當然) - 你可以在每個函數中將它們總結出來 - http://jsfiddle.net/2late2die/EWgzq/3/ – ilia

1

嘗試在$(document).ready(function(){...});包裝你的代碼。它可能是在DOM準備好之前執行的。

<script> 
//Returns how many correct answers were checked 
$(document).ready(function(){ 
    $('#submitButton').click(function(){ 
     var arrayScore = [];   
     var tempScore = 0; 
     $('.confirmContainer').remove(); 
     $('.toggleConfirmList:checked').each(function(){ 
      arrayScore.push($(this).val()); 
     }); 
     for(var i=0; i<arrayScore.length; i++){ 
      tempScore = arrayScore[i] + tempScore; 
     } 
     $('feedback').show(); 
     $("#scoreArea").val(tempScore);  
    }); 
}); 
</script> 
+0

感謝Knyri做到了!然而,由於某種原因,'scoreArea'元素不斷顯示評分爲我最初初始化'tempScore'時的任何值。我認爲我的數組似乎沒有推入任何內部元素。是否將行: $('。toggleConfirmList:checked')。each(function(){ 查找該列表中所有已選中的複選框? – user1410668

+0

您可以在for循環中提醒並檢查它是否正在進入循環。 – Imdad

0
<script> 
//Returns how many correct answers were checked 
$('#submitButton').click(function(){ 
    var arrayScore = [];   
    var tempScore = 0; 
    $('.confirmContainer').remove(); 
    $('.toggleInputContainer input[type="checkbox"]:checked').each(function(){ 
     arrayScore.push($(this).val()*1); // See the *1 
    }); 
    for(var i=0; i<arrayScore.length; i++) 
    { 
     tempScore = arrayScore[i] + tempScore; 

    } 

    $('div.feedback').show(); // added div. it was missing. 
    $("#scoreArea").html(tempScore); // it should be .html() or .text() not .val(). The .val() works only for form elements 

}); 
</script>