2016-12-05 51 views
0

我遇到问题。我有一个简单的计算器脚本,脚本开始计算不正确。如果我输入“1000”,它会计算正确的(10美元),如果我输入50000,它也可以正确计算(380美元)。但是如果我输入10000,它会计算错误。计算器的脚本错误计数范围

的范围是:

0 – 1000 = $0.01 
10000 – 10000 = $0.009 
10000 – 25000 = $0.0084 
25000 – 50000 = $0.0076 
50000+ = $0.0076 

可惜我不是一个JavaScript专家,所以我会感谢你的帮助。

function priceCalculation(a){ 
 
    if(a <= 1000){ 
 
     return 0.001; 
 
    }else if(a >= 1001 && a <= 10000){ 
 
     return 0.009; 
 
    }else if(a >= 10001 && a <= 25000){ 
 
     return 0.0084; 
 
    }else if(a >= 25001 && a <= 50000){ 
 
     return 0.0076; 
 
    }else{ 
 
     return 0.0076; 
 
    } 
 
} 
 

 
// number format set to en-US e.g (from 1500 to 1,500) 
 
var numFormat = new Intl.NumberFormat("en-US"); 
 

 
$('#likecount').keyup(function(e){ 
 
    // if a '.' is pressed 
 
\t if($(this).val().endsWith('.')) { 
 
    \t return; 
 
    } 
 

 
    // if input value is empty then assign '0' else the original value 
 
    var inputVal = $(this).val() === ''?'0':$(this).val(); 
 
    
 
    inputVal = parseFloat(inputVal.replace(/[$|,]/g, '')); 
 
    var price = priceCalculation($(this).val()); 
 
    var total = (inputVal * price); 
 
    total = (Math.round(total * 100)/100).toFixed(2); 
 
    var formatted = numFormat.format(inputVal) // set format to input 
 
    $(this).val(formatted); // display the formatted input back 
 
    $('#output').text((total)); // display the total price 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<input placeholder="How many Instagram likes?" style="height: 50px;width: 360px;color: #222;border-radius: 5px;border: 1px #85c9e3 solid;font-size: 18px;" type="text" id="likecount" /> 
 
<p style="font-family: 'Montserrat', sans-serif; padding: 20px 0px; font-weight: bold; color: #222; ">Pricing: <b><span style="color: #004f04;"> $</span><span id="output" style="color: #004f04;"></span></b></p>

+0

当您输入'10,000'时,它将输入为'1,0000' – philantrovert

回答

1

此行是无效的。

var price = priceCalculation($(this).val()); 

$(this).val()返回一个字符串。当你在它,(上千种),它会导致打电话给你priceCalculation功能和返回最后else声明这是return 0.0076;

既然你已经有了inputVal你已经解析成数,你可以使用它像,

var price = priceCalculation(inputVal); 

最终结果,

function priceCalculation(a) { 
 
    if (a <= 1000) { 
 
    return 0.001; 
 
    } else if (a >= 1001 && a <= 10000) { 
 
    return 0.009; 
 
    } else if (a >= 10001 && a <= 25000) { 
 
    return 0.0084; 
 
    } else if (a >= 25001 && a <= 50000) { 
 
    return 0.0076; 
 
    } else { 
 
    return 0.0076; 
 
    } 
 
} 
 

 
// number format set to en-US e.g (from 1500 to 1,500) 
 
var numFormat = new Intl.NumberFormat("en-US"); 
 

 
$('#likecount').keyup(function(e) { 
 
    if ($(this).val().endsWith('.')) { 
 
    return; 
 
    } 
 
    
 
    var inputVal = $(this).val() === '' ? '0' : $(this).val(); 
 

 
    inputVal = parseFloat(inputVal.replace(/[$|,]/g, '')); 
 
    var price = priceCalculation(inputVal); 
 
    var total = (inputVal * price); 
 
    total = (Math.round(total * 100)/100).toFixed(2); 
 
    var formatted = numFormat.format(inputVal); 
 
    $(this).val(formatted); 
 
    $('#output').text((total)); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<input placeholder="How many Instagram likes?" style="height: 50px;width: 360px;color: #222;border-radius: 5px;border: 1px #85c9e3 solid;font-size: 18px;" type="text" id="likecount" /> 
 
<p style="font-family: 'Montserrat', sans-serif; padding: 20px 0px; font-weight: bold; color: #222; ">Pricing: <b><span style="color: #004f04;"> $</span><span id="output" style="color: #004f04;"></span></b> 
 
</p>

+0

非常感谢,@choz!它完美的作品。 – Morgari

1

你犯了一个小错误

function priceCalculation(a){ 
 
    if(a <= 1000){ 
 
     return 0.001; 
 
    }else if(a >= 1001 && a <= 10000){ 
 
     return 0.009; 
 
    }else if(a >= 10001 && a <= 25000){ 
 
     return 0.0084; 
 
    }else if(a >= 25001 && a <= 50000){ 
 
     return 0.0076; 
 
    }else{ 
 
     return 0.0076; 
 
    } 
 
} 
 

 
// number format set to en-US e.g (from 1500 to 1,500) 
 
var numFormat = new Intl.NumberFormat("en-US"); 
 

 
$('#likecount').keyup(function(e){ 
 
    // if a '.' is pressed 
 
\t if($(this).val().endsWith('.')) { 
 
    \t return; 
 
    } 
 

 
    // if input value is empty then assign '0' else the original value 
 
    var inputVal = $(this).val() === ''?'0':$(this).val(); 
 
    
 
    inputVal = parseFloat(inputVal.replace(/[$|,]/g, '')); 
 
    var price = priceCalculation(inputVal); //a little mistake ;D 
 
    var total = (inputVal * price); 
 
    total = (Math.round(total * 100)/100).toFixed(2); 
 
    var formatted = numFormat.format(inputVal) // set format to input 
 
    $(this).val(formatted); // display the formatted input back 
 
    $('#output').text((total)); // display the total price 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<input placeholder="How many Instagram likes?" style="height: 50px;width: 360px;color: #222;border-radius: 5px;border: 1px #85c9e3 solid;font-size: 18px;" type="text" id="likecount" /> 
 
<p style="font-family: 'Montserrat', sans-serif; padding: 20px 0px; font-weight: bold; color: #222; ">Pricing: <b><span style="color: #004f04;"> $</span><span id="output" style="color: #004f04;"></span></b></p>