讓我們這個一步一步一次。
您開始使用:
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