使用 jQuery 加减百分比

Add and Subtract Percentages using jQuery

我在尝试使用 jQuery 加减百分比时遇到困难。也许我只是没有理解它,对此我深表歉意。

已更新。工作解决方案: JsFiddle

HTML

<div class="number-forecast">New number will replace this. </div>

<div id="numbers">
  Number: Example: 0.14556416<br/>
  <input class="thenumber" type="tel" maxlength="10" pattern="^\d{2}-\d{3}$" onkeypress="return isNumeric(event)" oninput="maxLengthCheck(this)" value="0.14556416" />
</div>
<div id="percentages">
  Percentage: From -100% to 9999%<br/>
  <input class="thepercentage" type="tel" maxlength="4" pattern="^\d{2}-\d{3}$" onkeypress="return isNumeric(event)" oninput="maxLengthCheck(this)" />
</div>

对于这个例子,我希望能够实现三件事。

  1. 增加 10% 应该得到 0.160120576
  2. 加上 998% 应该等于:1.5982944768
  3. 从中减去百分比。示例:-30%,结果应为:0.101894912.

我已尝试使用以下代码执行此操作,但找不到任何地方,或遗漏了什么?

    $('.thepercentage').keyup(function(e){

        thenum = $('.thenumber').val();
        thepercentage = $('.thepercentage').val();

        setTimeout(function() {
            if(parseInt(thepercentage) > 0.01) {  
            var thepercentagenum = $(".thepercentage").val();
            var numberhere = parseInt($(".thenumber").val(), thepercentagenum);
                            var thereplacement = (numberhere * thepercentagenum/100); // percent means divide by 100
                $('.number-forecast').text(thereplacement);
            } else {
            var thepercentagenums = $(".thepercentage").val();
            var numberheres = parseInt($(".thenumber").val(), thepercentagenums);
                            var thereplacements = -(numberheres * thepercentagenums/100); // percent means divide by 100
                $('.number-forecast').text(thereplacements);
            }

            }, 500);

    });
});

所以我使用了 keyup 函数来检测变化。然后我检索了 .thenumber' and.thepercentageasvaluesthen used anif` 语句来计算如果我需要一个加或减符号,如果它低于或超过 0.00%。

请找到我尝试的 JSFiddle:JSFiddle here

您的 JavaScript 有几个问题。这是我看到的两个主要问题:

  • 您正在使用 parseInt 解析浮点数 (0.14),因此您失去了精度并得到 0。
  • 未定义 isNumeric 和 maxLengthCheck 等方法。事实上,你甚至不需要它们。

这是工作代码:

$(document).ready(function (){
    $('.thepercentage').keyup(function(e){ 

        thenum = $('.thenumber').val();
        thepercentage = $('.thepercentage').val();

        setTimeout(function() {
            if(parseInt(thepercentage) > 0.01) {  
            var thepercentagenum = parseFloat($(".thepercentage").val());
            var numberhere = parseFloat($(".thenumber").val());
            console.log(thepercentagenum + "and" + numberhere);
                            var thereplacement = (numberhere * thepercentagenum/100.0); // percent means divide by 100
                $('.number-forecast').text(thereplacement);
            } else {
            var thepercentagenums = $(".thepercentage").val();
            var numberheres = parseInt($(".thenumber").val());
                            var thereplacements = -(numberheres * thepercentagenums/100); // percent means divide by 100
                $('.number-forecast').text(thereplacements);
            }

            }, 500);

    });
});

Here 是正确工作的 fiddle。祝你好运!