让我们这个一步一步一次。
您开始使用:
var xxxx = 0;
$('.clonedInput').each(function(index) {
if($(this).children().filter(':checked').length == 2)
xxxx++;
});
对我来说这看起来像你只是想filter
.clonedInput
元素的集合,并找出有多少匹配滤波器:
var xxxx;
function hasTwoCheckedChildren(i) {
return $(this).children(':checked').length == 2;
}
xxxx = $('.clonedInput').filter(hasTwoCheckedChildren).length;
由其次:
var num_normal_foods = 0;
$('[id^="amount_"]').each(function(index) {
if($(this).val() == '30.00')
num_normal_foods++;
});
再次,这看起来像一个过滤器环功能对我说:
var num_normal_foods;
function valueIsThirty(i) {
return +$(this).val() === 30;
}
num_normal_foods = $('[id^="amount_"]').filter(valueIsThirty).length;
最后,重要的是,该代码做你想让它做什么。如果你用.each
编写的代码做了你想要的代码,那么就没有必要改变它。无论如何,幕后花絮filter
使用each
。
那么使用'.each()'有什么不好?不管你如何编写这段代码,你都会在某个地方执行一个隐式循环。 – Blazemonger 2012-07-19 13:27:11
张贴HTML也是,plz。 – F0G 2012-07-19 13:31:54