【发布时间】:2015-08-22 09:44:56
【问题描述】:
所以...我在这里有这段代码,我正在使用它来进行测验,我想知道我是否可以使它更有效...最重要的是,如果可能的话,我希望代码更短。如果你有什么建议请告诉我!
$("#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++;
}
});
【问题讨论】:
-
所以你的代码 sn-ps 之间的唯一区别是 question 之后的数字。尝试使用 for 循环并循环 8 次?
标签: jquery performance