2015-12-17 25 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