让我们一步一步来。
你开始:
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