2011-11-30 94 views
2
for (i = 1; i < total.length; i++) { 
    $("div.category:nth-of-type(" + i + ") ul.qItem li :input[type=radio]:checked").each(function() { 
     total[i] += parseInt($(this).val()); 
     $("div.category:nth-of-type(" + i + ") div.report div.scorebox").html(total[i] + "/15"); 
    }); 
    if (total[i] > 11) { 
     $("div.category:nth-of-type(" + i + ") div.t12").toggle(); 
     $("div.category:nth-of-type(" + i + ") div.report div.scorebox").css("color", "green"); 
     $("div.category:nth-of-type(" + i + ") div.report span.opinion").html(high).css("color", "green"); 
    } else if (total[i] < 7) { 
     $("div.category:nth-of-type(" + i + ") div.t6").toggle(); 
     $("div.category:nth-of-type(" + i + ") div.report div.scorebox").css("color", "#900"); 
     $("div.category:nth-of-type(" + i + ") div.report span.opinion").html(low).css("color", "#900"); 
    } else { 
     $("div.category:nth-of-type(" + i + ") div.t711").toggle(); 
     $("div.category:nth-of-type(" + i + ") div.report div.scorebox").css("color", "orange"); 
     $("div.category:nth-of-type(" + i + ") div.report span.opinion").html(medium).css("color", "orange"); 
    } 
} 

这个for循环应该遍历5个div并且总结其中每个选择的输入单选按钮的值。然后它应该在评分箱中输出5个类别中的每个类别的分数。根据3个范围,它应该输出关于他们的表现(低,中,高)的不同观点,给文本加上红色,橙色或绿色,并且切换一些文本提供建议的可见性(t6,t711,t12)。不幸的是,它似乎无法将评分放入评分框或选择正确的文字来显示。任何人都可以建议如何让这个脚本依次在每个div上运行?遍历整个div

+1

请张贴例如HTML太 –

+0

您的代码看起来不错,只要这两个假设是正确的:1)你有插件或扩展的jQuery以支持nth类型的选择器,2)你的HTML的结构是一堆'category'divs,在每个div里面都是'advice'div和'report'div;在'报告'div里面是'记分框'和'意见'。 – rkw

回答

4

jQuery就有这个建于

$('selector').each(function(index, element) { 

}); 

$(selector).each()

+0

有用的OP知道,但它实际上并没有帮助的问题。 'for'循环是一个有效的,有时候是可取的迭代器,它的执行效率远高于'.each()' –

+0

“任何人都可以建议如何让这个脚本依次在每个div上运行? OP没有提及性能。这是无关紧要的。 – liammclennan

+0

这不是我的意思。我也使用'.each()'。我只是说,目前还不清楚是否有一个“选择器”,他可以使用这将导致5次迭代。 ;-)他的索引基于一个数组数组的长度,这可能与选择器对象数组相关或不相关。无论哪种方式,这不是'for'循环本身就是问题所在。尽管最后一句的措辞,似乎问题不是“我该如何迭代?” ('for'循环很好),而是“为什么scorebox没有被填充并且文本以5遍更新?” –