所以...我在这里有这个代码,我正在使用一个测验,我想知道如果我可以使它更有效率...所有我希望代码缩短如果可能。如果你有一些建议,请告诉我!使片段效率更高
$("#done").click(function(){
var c1 = 0,c2 = 0,c3 = 0;
switch ($("input[name='question1']:checked").val()){
case 1: c1++;
break;
case 2: c2++;
break;
case 3: c3++;
}
//repeats 6 times with the number of the question incremented every time
switch ($("input[name='question8']:checked").val()){
case 1: c1++;
break;
case 2: c2++;
break;
case 3: c3++;
}
});
因此,您的代码片段之间的唯一区别是* question *之后的数字。尝试使用for循环和循环8次? –