为什么我在这个Javascript代码输出错误?

时间:2015-04-12 12:57:14

标签: javascript jquery onkeypress

我有3个HTML输入文本,当我尝试输入付费输入文本时,我得到的答案只有100到999,但当应付总金额达到1000+时输出错误。

示例:

应付金额: 500 (1000以上输出错误)

付费: 600 < - 当我输入600时,它有正确的输出要更改,只要它从0到999的应付金额。

更改 100 < - 正确。

..但是当应付金额高于999时,输出错误。

示例:

应付金额: 1100

付费: 1200

更改: 1199< - 现在输错了。因为应付金额高于1000。

这是我的剧本:

$("#payable_form input").each(function(){
    $(this).keyup(function(){
       var payable_amount;
       var paid;

       payable_amount = parseFloat( $('#payable_amount').val() );
       paid = parseFloat( $('#paid').val() );
       payable_amount = $.isNumeric( payable_amount ) ? payable_amount : 0;
       paid = $.isNumeric(paid) ? paid : 0;

       var change = paid - payable_amount;
       $('#change').val(change.toFixed(2));
   });
});

完整代码:

<form class="form-horizontal" action="salestranslog.php" id="payable_form" method ="POST">
  <br />
    <div class="form-group">
      <label class="control-label col-xs-4">Customers:</label>
            <div class="col-xs-6">
        <?php  
             $result = mysqli_query($connection, "SELECT * FROM customers"); 
             echo "<select class='form-control' name='customer'>"; 
             while($row = mysqli_fetch_assoc($result)) 
             { 
                echo "<option value = '".$row[name]."'>".$row[name]."</option>"; 
             }
             echo "</select>"; 
        ?>
            </div>  
    </div>
    <div class="form-group form-group has-error has-feedback">
        <label class="control-label col-xs-4" for="amount">Total Payable Amount:</label>
        <div class="col-xs-4">
            <input type="text" class="form-control" id="payable_amount" name="payable_amount" value="<?php echo  number_format($total_sales,2); ?>"readonly>
            <span class="glyphicon glyphicon-info-sign form-control-feedback"></span>
        </div>
    </div>
    <div class="form-group">
        <label class="control-label col-xs-4" for="ttl_items">Total Purchase Items:</label>
        <label class="control-label col-xs-1" for="total_items"><mark><?php echo $count; ?></mark></label>
    </div>
        <div class="form-group">
      <label class="control-label col-xs-4">Paid by:</label>
            <div class="col-xs-6">
                <select class="form-control" name="cash">
                    <option>Cash</option>
                    <option>Cheque</option>
                </select>
            </div>  
    </div>

    <div class="form-group form-group-lg">
        <label class="control-label col-xs-4" for="paid">Paid:</label>
        <div class="col-xs-4">
            <input type="text" class="form-control" name="paid" id="paid" placeholder="0.00">
        </div>
    </div>
    <br /><br /><br />
    <div class="form-group has-warning has-feedback">
        <label class="control-label col-xs-4" for="ttl_items">Return Change:</label>
        <div class="col-xs-4">
            <input type="text" class="form-control" id="change" name="change" placeholder="0.00" readonly>
             <span class="glyphicon glyphicon-send form-control-feedback"></span>

        </div>
    </div>
    <br />
     </div>                     
    <script>
        $("#payable_form input").each(function(){
            $(this).keyup(function(){
            var payable_amount;
            var paid;
            payable_amount = parseFloat($('#payable_amount').val());
            paid = parseFloat($('#paid').val());
            payable_amount = $.isNumeric(payable_amount)?payable_amount:0;
            paid = $.isNumeric(paid)?paid:0;
            var change = paid - payable_amount;
            $('#change').val(change.toFixed(2));

        });
      });
    </script>

    <div class="modal-footer">
        <input type="submit" name="submit" value = "Submit" class="btn btn-success">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
    </div>
</form>

1 个答案:

答案 0 :(得分:2)

number_format为数字&gt; = 1000

插入逗号

number_format(1100, 2)打印“1,100.00”

运行parseFloat(“1,100.00”)将输出1和1200 - 1 = 1199。

这应该可以更好地使用parseFloat:

number_format($total_sales, 2, ".", "");