PHP数字格式不允许jquery sum

时间:2017-02-15 09:30:23

标签: javascript php jquery number-formatting

我在使用jquery中的数字进行计算时遇到问题。当数字是PHP数字格式时,jquery不会检测这些数字。请找到输出的代码和屏幕截图。任何帮助将不胜感激。

  <tr  class="noBorder">
  <td><span class="circle">1A</span></td>
  <td><input type="text" class="amnt" value="<?php echo number_format($a1,2);?>"/></td>
  </tr>
  <tr  class="noBorder">
  <td><span class="circle">4</span></td>
  <td><input type="text" class="amnt" id="4tax" /></td>
  </tr>
  <tr  class="noBorder">
  <td><span class="circle">5A</span></td>
  <td><input type="text" class="amnt" id="5a" /></td>
  </tr>
  <tr  class="noBorder">
  <td><span class="circle">7</span></td>
  <td><input type="text" name="7"  class="amnt"/></td>
  </tr>
  <tr  class="noBorder">
  <td><span class="circle">7A</span></td>
  <td><input type="text" name="7a" class="amnt" /></td>
  </tr>
  <tr  class="noBorder">
  <td>1A+4+5A+7+7A</td>
  <td><span class="circle">8A</span></td>
  <td>
<input type="text" id="8a" />
  </td>
  </tr>

Jquery的

$(".amnt").on("keydown keyup", function() {
        calculateSum();
    });
function calculateSum() {
    var sum2 = 0; 
    $(".amnt").each(function() {
        //add only if the value is number
        if (!isNaN(this.value) && this.value.length != 0) {
            sum2 += parseFloat(this.value);
            //$(this).css("background-color", "#FEFFB0");
        }

    });

}

输出: 的 8A = 1A + 4 + 5A + 7 + 7A enter image description here

3 个答案:

答案 0 :(得分:2)

Javascript无法将,中的数字作为千位分隔符处理。它不是数字,而是人格式的字符串。请执行以下操作

<?php echo number_format($a1, 2, '.', '');?>

UPD。在js中,您可以使用,删除this.value.replace(/,/g, "")以删除逗号:

parseFloat("12,233.12")
>12
parseFloat("12,233.12".replace(/,/g,""))
>12233.12

$(".amnt").on("keydown keyup", function() {
        calculateSum();
    });
function calculateSum() {
    var sum2 = 0; 
    $(".amnt").each(function() {
        var thevalue = this.value.replace(/,/g,'');

        if (!isNaN(thevalue ) && thevalue .length != 0) {
            sum2 += parseFloat(thevalue);
            $("#sum").text(sum2).css("background-color", "#FEFFB0");
        }
    });

}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type = "text" class= "amnt" value = "8,133,231.17"><br/>
<input type = "text" class= "amnt" value = ""><br/>
<input type = "text" class= "amnt" value = ""><br/>
<div id = "sum"></div>

答案 1 :(得分:2)

请检查一下: -

$(".amnt").on("keydown keyup", function() {
    calculateSum();
});
function calculateSum() {
var sum2 = 0; 
$(".amnt").each(function() {
    //add only if the value is number
    if (this.value.indexOf(',') > -1) { 
      sum2  += parseFloat(this.value.split(",").join(""));
    }else if(!isNaN(this.value) && this.value.length != 0) {
        sum2 += parseFloat(this.value);
        $("#sum").text(sum2).css("background-color", "#FEFFB0");
    }
});

}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type = "text" class= "amnt" value = "8,133,231.17"><br/>
<input type = "text" class= "amnt" value = ""><br/>
<input type = "text" class= "amnt" value = ""><br/>
<div id = "sum"></div>

答案 2 :(得分:0)

我建议您使用Java而不是PHP显示数字。首先在您的html中设置value =“”

    $(document).ready(function() {
    //use json_encode to make the php script readable by javascript
     var amntval = Number(<?php echo json_encode($a1,2);?>); 

    //display the value in the amnt field 
    $(".amnt").val(amntval.toLocaleString('en'));

    //now run your calculations
    $(".amnt").on("keydown keyup", function() {
        calculateSum();
    });
    function calculateSum() {
         var sum2 = 0; 
         $(".amnt").each(function() {
         //add only if the value is number
        if (!isNaN(this.value) && this.value.length != 0) {
            sum2 += parseFloat(this.value);
           //$(this).css("background-color", "#FEFFB0");
    }

});

}


});

这对我有用,我没有用您的代码对其进行过测试,但我认为它应该可以工作!