Javascript 循环通过选中的复选框,然后显示结果

Javascript 循环通过选中的复选框,然后显示结果,javascript,jquery,html,Javascript,Jquery,Html,我正在做一个练习,用户必须检查列表中的正确答案,然后按submit查看他/她做得有多好。但是,我的提交按钮似乎不起作用。单击时不会发生任何事情。以下是脚本: <script> //Returns how many correct answers were checked $('#submitButton').click(function(){ var arrayScore = []; var tempScore = 0; $('.confirm

我正在做一个练习,用户必须检查列表中的正确答案,然后按submit查看他/她做得有多好。但是,我的提交按钮似乎不起作用。单击时不会发生任何事情。以下是脚本:

<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>

//返回检查了多少正确答案
$(“#提交按钮”)。单击(函数(){
var arrayScore=[];
var-tempScore=0;
$('.confirmContainer').remove();
$('.toggleConfirmList:checked')。每个(函数(){
arrayScore.push($(this.val());
});

对于(var i=0;i尝试将代码包装在
$(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>

//返回检查了多少正确答案
$(文档).ready(函数(){
$(“#提交按钮”)。单击(函数(){
var arrayScore=[];
var-tempScore=0;
$('.confirmContainer').remove();
$('.toggleConfirmList:checked')。每个(函数(){
arrayScore.push($(this.val());
});
对于(变量i=0;i

//返回检查了多少正确答案
$(“#提交按钮”)。单击(函数(){
var arrayScore=[];
var-tempScore=0;
$('.confirmContainer').remove();
$('.toggleInputContainer输入[type=“checkbox”]:选中”)。每个(函数(){
arrayScore.push($(this.val()*1);//参见*1
});

对于(var i=0;i对于span元素的值设置,应使用
text()
方法,而不是
val()

//返回检查了多少正确答案
$(“#提交按钮”)。单击(函数(){
var arrayScore=[];
var-tempScore=0;
$('.confirmContainer').remove();
$(':checkbox:checked')。每个(函数(){//用于选择可使用的复选框):checkbox“选择器
arrayScore.push(parseInt($(this.val());//parseInt()值
});

对于(var i=0;i

$('feedback')).show();
应该是
$('.feedback').show()
感谢Knyri这么做!但是由于某种原因,'scoreArea'元素不断地将分数显示为我最初初始化为'tempScore'的值。我认为我的数组似乎没有将任何元素推入其中。行:$('.toggleConfirmList:checked')。each(function(){找到该列表中所有选中的复选框?您可以在for loop中发出警报,并检查它是否正在进入循环或不完美,非常感谢。我知道这些错误看起来很可怜,但这是我的第一个合作期,我一生中从未做过web开发:(@user1410668不客气,不,这些是常见错误,祝你好运。我还可以建议一些调整。首先尝试使用
。toggleConfirmList输入:checked
选择器,因为它会更快更清晰,而且你可能根本不需要将值放入数组中(当然,这取决于你的总体需要)-您可以在每个函数中对它们进行汇总-
<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>
<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>
//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()    

});