2015-12-17 30 views
0

我有一家提供10%預付折扣的汽車租賃公司。我已經正確地格式化了總價格,但是當有人向其中添加「額外」時,它會停止將價格總額更改爲10%。更改動態價格的問題

這是價格領域:

<span id="cashamount" class="additional xxlarge carrental_security_deposit" data-deposit="<?php echo $available_payments['carrental-paypal-security-deposit'];?>" data-deposit-round="<?php echo $available_payments['carrental-paypal-security-deposit-round'];?>"> - </span> <span class="additional xxlarge">ISK</span> 

這是JavaScript我使用:

<script type="text/javascript">//<![CDATA[ 
window.onload=function(){ 
Number.prototype.formatMoney = function(c, d, t){ 
var n = this, 
    c = isNaN(c = Math.abs(c)) ? 2 : c, 
    d = d == undefined ? "." : d, 
    t = t == undefined ? "," : t, 
    s = n < 0 ? "-" : "", 
    i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", 
    j = (j = i.length) > 3 ? j % 3 : 0; 
    return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : ""); 
}; 

var Total = $("#cashamount"); 
var totalNumber = Number(Total.text().replace(/[^0-9\.]+/g,"")); 
Total.text((totalNumber * 1.1).formatMoney(2, '.', ',')); 



}//]]> 

</script> 

它的存在,平變化的變量或東西,因此監控更改價格字段和變化? 。

對此非常感謝。

+0

單字母的變量使你的代碼難以閱讀,您可能需要考慮使用更多描述性變量名稱。 – apaul

+0

是@ apaul34208,我會這樣說的,並且:你不要一直重命名這些變量。 '價= 15;價格=價格+ 15;'這使得價格''30'。 – Matt

回答

1

您可以使用jQuery的每一個具體的事件被觸發時執行的函數,你的情況有點像jQuery的change功能應該做的伎倆:

$('#idOfPriceField').change(function() { 
    // make the desired updates here 
}); 
+0

因爲我在這個OP的代碼中沒有看到任何jQuery。我想指出,id會寫成'$(「#id」)'注意'#'。 – Matt

+0

@Matt好,我已經更新了我的答案 – bwegs