我的项目中有销售交易,我想计算有和没有
gst
的总金额。
如果我们希望输入特定产品的
textbox
值,同时如果我们不想包含要自动计算的总金额,则在按键时自动计算,我会保留 gst
来输入 gst
值。
JavaScript:
$(document).ready(function() {
$('#gst').keyup(function(ev) {
var reeta = price * qty;
var tot_price = (reeta * gst / 100) + reeta ;
var divobj = document.getElementById('tot_amount');
divobj.value = tot_price;
});
});
PHP:
<tr>
<td>
<?php echo ++$counter; ?>
</td>
<td class="record">
<?php echo $row['prod_name'];?>
</td>
<td>
<input class="w3-input w3-border" name="pages" id="qty" type="text" readonly value="<?php echo $row['qty'];?">
</td>
<td>
<input class="w3-input w3-border" name="pages" id="price" type="text" readonly value="<?php echo number_format($row['price'], 2);?>">
</td>
<td>
<input class="w3-input w3-border" name="pages" id="gst" type="text"><br><br>
</td>
<td>
<input class="w3-input w3-border" name="tot_amount" readonly id="tot_amount" type="text" ><br><br>
</td>
</tr>
因此我需要根据输入
gst
字段的汇率来计算 gstrate
。有人可以帮我吗?大多数公式都会计算 gst
金额并将其添加到小计中,但我没有机会这样做。
我不知道你为什么使用ids,这是例子
$(document).ready(function() {
$('#gst').keyup(function(ev) {
var gst = $("#gst").val();
var price = $("#price").val();
var qty = $("#qty").val();
var reeta = price * qty;
var tot_price = (reeta * gst / 100) + reeta;
var divobj = document.getElementById('tot_amount');
divobj.value = tot_price;
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<tr>
<td>
1
</td>
<td class="record">
AMK
</td>
<td>
<input class="w3-input w3-border" name="pages" id="qty" type="text" readonly value="2">
</td>
<td>
<input class="w3-input w3-border" name="pages" id="price" type="text" readonly value="144.00">
</td>
<td>
<input class="w3-input w3-border" name="pages" id="gst" type="text"><br><br>
</td>
<td>
<input class="w3-input w3-border" name="tot_amount" readonly id="tot_amount" type="text"><br><br>
</td>
</tr>
这就是实现它的方法:
$('#gst').keyup(function(ev) {
var price = $('#price').val();
var qty = $('#qty').val();
var gst = $(this).val();
var reeta = price * qty;
var tot_price = (reeta * gst / 100) + reeta ;
$('#total').val(tot_price);
});
检查我的工作小提琴 https://jsfiddle.net/nkvhz758/
我最好建议您对结果的焦点事件进行更改,这样听起来才合适。
试试这个代码,它可以工作..!
此代码可工作任何值输入时间更改总计它是动态代码
$(document).ready(function() {
$('#gst').keyup(function(ev) {
cal();
});
$('#price').keyup(function(ev) {
cal();
});
$('#qty').keyup(function(ev) {
cal();
});
function cal()
{
var gst = 0;
var price = 0;
var qty = 0;
var amount = 0;
var total = 0;
qty = $('#qty').val();
if(qty > 0)
{
price = $("#price").val();
if(price > 0)
{
amount = parseFloat(qty) * parseFloat(price)
gst = $("#gst").val();
if(gst > 0)
{
gst = parseFloat(amount) * parseFloat(gst) / 100;
total = parseFloat(amount) + parseFloat(gst);
$('#total').val(total);
$('#total_gst').val(gst);
}
else
{
total = parseFloat(amount);
$('#total').val(total);
$('#total_gst').val(gst);
}
}
}
}
});
table td
{
border: 1px solid #000;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<tr>
<td>1</td>
<td class="record">AMK</td>
</tr>
<tr>
<td>Enter The Qty</td>
<td><input class="w3-input w3-border" name="pages" id="qty" type="text" value="10"></td>
</tr>
<tr>
<td>Enter The Price</td>
<td><input class="w3-input w3-border" name="pages" id="price" type="text" value="100"></td>
</tr>
<tr>
<td>Enter The GST</td>
<td><input class="w3-input w3-border" name="pages" id="gst" type="text" value="0"></td>
</tr>
<tr>
<td>Total The GST</td>
<td><input class="w3-input w3-border" name="pages" id="total_gst" type="text" value="0" disabled></td>
</tr>
<tr>
<td>Total</td>
<td><input class="w3-input w3-border" name="total" disabled id="total" type="text"></td>
</tr>
</table>
10 100×( 100 10 )=10 美元。