我使用下面的函數來計算產品的總價格:爲什麼jQuery在應用函數之前清空div?
jQuery(document).ready(function() {
function j_updateprice() {
var p = 0;
jQuery('.am-signup-form input[type=radio][id^=product]:checked,
.am-signup-form input[type=checkbox][id^=product]:checked')
.each(function(i, v) {
if (jQuery(v).attr('data-first_price')) {
p += parseFloat(jQuery(v).attr('data-first_price'));
}
});
jQuery('#j_price').text(p.toFixed(2));
}
jQuery('.am-signup-form input[type=radio][id^=product],
.am-signup-form input[type=checkbox][id^=product]')
.change(function() {
j_updateprice();
});
j_updateprice();
});
的問題是,當我在另一個div的,價格只是不斷累加的複選框/單選按鈕點擊(它不該「T)。所以,我認爲j_updateprice()
被應用前的總(#j_price
)排空:
jQuery('.am-signup-form input[type=radio][id^=product],
.am-signup-form input[type=checkbox][id^=product]')
.change(function() {
jQuery('#j_price').empty();
j_updateprice();
});
但沒有任何反應。價格只是加起來。這是爲什麼?
直播現場:http://www.chineselearnonline.com/amember/signup
您需要在取消選中某個項目時從「p」變量中減去。 –
沒有關係,但我覺得'。數據(「first_price」)'是在參訪'.attr(「數據first_price」)' – RRK
@Joshua Coussard對不起,我沒有很好地理解。你能提供一個例子嗎? – alexchenco