我有一个问题。我有一个简单的计算器脚本,脚本开始计算错误。如果我输入" 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
if($(this).val().endsWith('.')) {
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
});
&#13;
<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>
&#13;
答案 0 :(得分: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
if($(this).val().endsWith('.')) {
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
});
&#13;
<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>
&#13;
答案 1 :(得分: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));
});
&#13;
<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>
&#13;