价格计算(基于用户输入金额水平)

时间:2013-11-02 01:47:21

标签: javascript jquery

我的代码基于这个jsfiddle:

http://jsfiddle.net/vxupE/

我希望将这些价格水平实施到我的代码中,以便当一个人投入501美元时,他们会收取20%的费用,但如果他们投入500美元则会收取25%的费用

if $price =< 101 then *.30
if $price > 100 and < 501 then *.25
if $price > 500 and < 1501 then *.20
if $price > 1500 then *.15

在javascript方面我很茫然。我认为这可以帮助其他人。肯定会帮助我! :)

2 个答案:

答案 0 :(得分:1)

试试这个:

$('#sub_tot').change(function(){
    var value = $(this).val();
    var charge = 0;
    if ( value >= 501 ) {
     charge = .25;   
    }
    else if ( value >= 100 ) {
     charge = .20;  
    }
    $('#tax').val( (value * charge).toFixed(2) );
}).change()

答案 1 :(得分:0)

我会考虑两种方式。

切换语句,有很多教程在线查看。另一种选择是使用if和else if if找到正确的额外费用。

if($price =< 101){
   charge=0.3;
}else if($price>=100){
   charge=0.25;
}else if($price>=500){
   charge=0.2;
}else if($price>=1500){
   charge=0.15;

然后可以使用费用来计算最终费用。

更新

您的代码正在检查您的子总值是否等于您的邮政编码。改变

$('#sub_tot').change(function(){$('#zip').change(function(){ to

相关问题