Jquery丢失小数值

时间:2017-03-10 14:12:34

标签: javascript jquery

我有发票表格,在零件部分,我丢失了2点小数值。有任何想法吗?请参阅下面的代码和屏幕截图。

<script>
 function doCalcoriginal() {
 var total = 0;

 $('tr').each(function() {
     $(this).find('span.amount').html($('input:eq(2)', this).val() * $('input:eq(3)', this).val());
 });
 $('.amount').each(function() {
     total += parseInt($(this).text(),10);

 });
 $("#partstotalvalue").val(total);

}

</script>

HTML

<tr>
    <td><input type="text" class="input-small" name="partnumber[]"></td>
    <td><input type="text" class="input-small" name="partdescription[]" ></td>
    <td><input type="text" class="input-small" name="partprice[]"   size="4" onblur="doCalcoriginal(); calculate(); "></td>
    <td><input type="text" class="input-small" name="partquantity[]"   size="4" onblur="doCalcoriginal(); calculate(); showMessage(); "></td>
    <td><input type="hidden" readonly class="partdb" size="4" name="partdb[]" style="background-color: transparent;border: 0px solid;" >
    <td><span class="amount" ></span> 
    <td><a class="deleteRow2"></a></td>
</tr>

 <div class="form-group">
 <label for="partstotalvalue" class="col-sm-3 control-label">Parts Total</label>
 <div class="col-sm-9">
 <input type="number" name="partstotalvalue" class="form-control" id="partstotalvalue" placeholder="Parts Total" readonly >
 </div>
 </div>
 <div class="form-group">
 <label for="partstax" class="col-sm-3 control-label">Parts Tax</label>
 <div class="col-sm-9">
 <input type="number" name="partstax" class="form-control" id="partstax" placeholder="Parts Tax" readonly >
 </div>
 </div>
 <div class="form-group">
 <label for="partstotalwithtax" class="col-sm-3 control-label">Parts Total with Tax</label>
 <div class="col-sm-9">
 <input type="number" name="partstotalwithtax" class="form-control" id="partstotalwithtax" placeholder="Parts Total with Tax" readonly >
 </div>
 </div>

Calc()函数正在执行税收,并且工作正常。

Screenshot Click Here

1 个答案:

答案 0 :(得分:2)

您应该使用parseFloat代替parseInt,请参阅下面的代码

$('.amount').each(function() {
     total += parseFloat($(this).text());
 });