我只想在 Balnce Row 时通过jQuery显示 Balnce Row 负整数值 Balnce Row < / strong>具有正整数值,而不是显示客户名称行。
多次使用if / else条件后我会尝试这样做,但是不能得到我所需的结果。请帮助我,解决我的问题。我的 JSFIDDLE 就是这个。并且代码在下面给出。 我非常感谢你。 :)
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$('.pay').change(function(){
var pay = $(this).val()-0;
var subtoal = $('.subtotal').val() - 0;
$('.payback').val(pay-subtoal);
});
$('.custname').change(function(){
var n= $('.payback').val()-0;
if(n>=0) {
$('.custname').hide(); //...Do stuff for +ve num
} else {
$('.custname').show(); //...Do stuff for -ve num
}
});
});
</script>
</head>
<body>
<form action="" method="post" >
<table class="table table-bordered ">
<tbody>
<tr>
<td colspan="0"><b>SubTotal :</b></td>
<td><input type="text" name="subtotal" class="form-control subtotal"></td>
</tr>
<tr class="pnotes">
<td colspan="0"><b>Cash Receive:</b></td>
<td colspan="0"><input type="text" name="pay" class="form-control pay">
</tr>
<tr class="pnotes">
<td colspan="0"><b>Balnce : </b></td>
<td><input type="text" name="payback" class="form-control payback"></td>
</tr>
<tr class="custname">
<td colspan="0"><b>Customer Name</b></td>
<td colspan="0"><input type="text" name="custname" class="form-control"></td>
</tr>
</tbody>
</table>
</form>
</body>
</html>
答案 0 :(得分:2)
希望这会有所帮助。您可以在页面加载时隐藏客户名称字段,然后根据“余额”字段中显示的值显示该字段。代码评论如下。
$('.pay').change(function () {
var pay = $(this).val() - 0;
var subtoal = $('.subtotal').val() - 0;
$('.payback').val(pay - subtoal).trigger("blur"); //Added .trigger("blur")
});
//Modified code below
$('.payback').on("blur", function () {
var n = $.isNumeric($(this).val()) ? $(this).val() - 0 : 0;
if (n >= 0)
$('.custname').hide();//...Do stuff for +ve num
else
$('.custname').show();//...Do stuff for -ve num
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<form action="" method="post">
<table class="table table-bordered ">
<tbody>
<tr>
<td colspan="0">
<b>SubTotal :</b>
</td>
<td>
<input type="text" name="subtotal" class="form-control subtotal">
</td>
</tr>
<tr class="pnotes">
<td colspan="0">
<b>Cash Receive:</b>
</td>
<td colspan="0">
<input type="text" name="pay" class="form-control pay">
</tr>
<tr class="pnotes">
<td colspan="0">
<b>Balnce : </b>
</td>
<td>
<input type="text" name="payback" class="form-control payback">
</td>
</tr>
<tr class="custname" style="display:none;">
<td colspan="0">
<b>Customer Name</b>
</td>
<td colspan="0">
<input type="text" name="custname" class="form-control">
</td>
</tr>
</tbody>
</table>
</form>