Jquery 使用开关盒操纵单选按钮值

Jquery 使用开关盒操纵单选按钮值,jquery,Jquery,我有一个代码,它获取所选单选按钮的值,并使用开关盒对它们进行计数,然后找出哪个值最高。然而,我被困在switch语句中,因为每当值进入其中时,它就会变成NaN或undefined。我曾尝试使用parseInt,但不管它何时进入开关盒,它都会破坏我得到的值 以下是JS: $('#submit').click(function() { var choice1, choice2, choice3, choice4, choice5, choice6; if (($("input[

我有一个代码,它获取所选单选按钮的值,并使用开关盒对它们进行计数,然后找出哪个值最高。然而,我被困在switch语句中,因为每当值进入其中时,它就会变成NaN或undefined。我曾尝试使用parseInt,但不管它何时进入开关盒,它都会破坏我得到的值

以下是JS:

$('#submit').click(function() {  
    var choice1, choice2, choice3, choice4, choice5, choice6;

    if (($("input[name='qa1']:checked").length == "0")){
        alert("ANSWER ALL QUESTIONS!");
    }

    else{
        var answer1 = parseInt($("input[name='qa1']:checked").val());

        switch(answer1){
            case 1: choice1++; 
                    break;
            case 2: choice2++;
                    break;
        }
        alert(choice1);
//Closing braces
示例HTML:

<div class="a_choice" id="a1" >
        <img src="img/q1-1.jpg" class="a_image" alt=" "/>
        <input type="radio" class="button" id="btn1" name="qa1" value="1">
        <label for="btn1"></label>
    </div>
<div class="a_choice" id="a2" >
        <img src="img/q1-2.jpg" class="a_image" alt=" "/>
        <input type="radio" class="button" id="btn2" name="qa1" value="2">
        <label for="btn2"></label>
    </div>
<input type="button" id="submit" name="submit" value="View Result" />

有人能帮我吗?

这是因为您必须指定如下值:

var choice1=0, choice2=0, choice3=0, choice4=0, choice5=0, choice6=0;
您没有为变量赋值,而是增加变量,这就是为什么它们会显示NAN


嗨,曼瓦尔!我已经尝试过初始化选择变量的值,但不管它何时进入开关盒,它都会吐出NaN或undefined。谢谢,Manwal!事实证明,我在启动选择变量时删除了parseInt,这就是为什么它一直变为NaN。