为什么我在这个 Javascript 代码中得到了错误的输出?
Why I got wrong output in on this Javascript code?
我有 3 HTML 个输入文本,当我尝试输入付费输入文本时,我只得到了 100 到 999 之间的正确答案,但是当总应付金额达到 1000+ 时,它得到了错误的输出。
示例:
应付金额:500(1000以上输出错误)
Paid: 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>
number_format
为 >= 1000
的数字插入逗号
number_format(1100, 2)
打印“1,100.00”
运行 parseFloat("1,100.00") 将输出 1 和 1200 - 1 = 1199.
这应该与 parseFloat 一起工作得更好:
number_format($total_sales, 2, ".", "");
我有 3 HTML 个输入文本,当我尝试输入付费输入文本时,我只得到了 100 到 999 之间的正确答案,但是当总应付金额达到 1000+ 时,它得到了错误的输出。
示例:
应付金额:500(1000以上输出错误)
Paid: 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>
number_format
为 >= 1000
number_format(1100, 2)
打印“1,100.00”
运行 parseFloat("1,100.00") 将输出 1 和 1200 - 1 = 1199.
这应该与 parseFloat 一起工作得更好:
number_format($total_sales, 2, ".", "");