javascript - 计算器的脚本计算范围错误

标签 javascript jquery

我有一个问题。我有一个简单的计算器脚本,但该脚本开始计算不正确。如果我输入“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
});
<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>

最佳答案

该行无效。

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

$(this).val() 返回一个字符串。当您有 , 时(数千个),它会调用您的 priceCalculation 函数并返回最后一个 else 语句,即 返回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>

关于javascript - 计算器的脚本计算范围错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40968962/

相关文章:

php - 如何打印包含在页面中的 php 页面内容作为 jquery 弹出消息

javascript - jquery动画彩虹般的颜色变化

javascript - 如何使用 jqGrid editoptions 值的函数创建有效的字符串?

javascript - 根据输入的字符动态增加输入类型文本文本框的宽度

javascript - NodeJs后端代码结构

javascript - stopPropagation 函数读取未定义

jquery - 管理边框颜色的 CSS 技巧

javascript - 使用 Prototype 或纯 JavaScript 获取表单中的所有单选按钮?

jquery - 仅选择动态生成的表中的下一个 <tr>

javascript - 根据页面使用 javascript 更改 CSS 样式