我有3个输入
Grand Total Box
<input type="text" name="GrandTotal" id="GrandTotal" class=" form-control" placeholder="Grand Total" required />
首付款
<input type="text" name="DownPayment" id="downpayment" class=" form-control" placeholder="Down Payment" required />
状态框
<input type="text" name="Status" id="status" class="form-control" placeholder="Status">
因此,使用JavaScript自动填充了总的总输入,示例GrandTotal是 2500 ,然后我必须填写下付盒,当我输入(如 3000 )时,状态必须说已付款。但是,当我们填补下付时,例如 2000 状态必须说减去500
但现实是当我写下付款数字 5 时,它说还清了,甚至没有更改,即使我放了超过孙子的价值。但是当我删除值时,它说未定义
这是我的脚本
$('#downpayment').on('keyup keydown', function (
var $this = $(this),
GrandTotal = $("#GrandTotal").val(),
downpayment = $("#downpayment").val();
var value = GrandTotal - downpayment;
if (GrandTotal < downpaymentval) {
var status = "PAID OFF";
$('#status').val(status);
} else {
var status = "MINUS".value;
$('#status').val(status);
}
console.log(status);
});
谢谢,现在已经解决了,谢谢您回答
您的代码中有一些错字。当您获得数字并清空status
字段时,我添加了一些parseFloat
。
$("#downpayment").on("keyup keydown", function () {
var $this = $(this),
GrandTotal = parseFloat($("#GrandTotal").val()),
downpayment = parseFloat($("#downpayment").val());
// Empty status field
$("#status").val("");
// Returns if undefined
if (!downpayment || !GrandTotal) {
return;
}
// Your code
var value = GrandTotal - downpayment;
if (GrandTotal < downpayment) {
var status = "PAID OFF";
$("#status").val(status);
} else {
var status = "MINUS " + value;
$("#status").val(status);
}
console.log(status);
});
<input type="text" name="GrandTotal" id="GrandTotal" class=" form-control" placeholder="Grand Total" required />
<input type="text" name="DownPayment" id="downpayment" class=" form-control" placeholder="Down Payment" required />
<input type="text" name="Status" id="status" class="form-control" placeholder="Status">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
您需要将字符串转换为float,然后才能比较它们。这可以使用parseFloat
GrandTotal = parseFloat($("#GrandTotal").val());
downpayment = parseFloat($("#downpayment").val());
请参阅此处的JSFIDDLE
我已经为问题创建了解决方案,
<input type="text" name="GrandTotal" id="GrandTotal" class=" form-control" placeholder="Grand Total" required />
<input type="text" name="DownPayment" id="downpayment" class=" form-control" placeholder="Down Payment" required />
<input type="text" name="Status" id="status" class="form-control" placeholder="Status">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script type="text/javascript">
$('#downpayment').on('keyup keydown', function () {
var $this = $(this);
var GrandTotal = parseInt($("#GrandTotal").val());
var downpayment = parseInt($("#downpayment").val());
var value = GrandTotal - downpayment;
if (GrandTotal <= downpayment) {
var status = "PAID OFF";
$('#status').val(status);
} else {
var status = "MINUS" + value;
$('#status').val(status);
}
console.log(status);
});
</script>
谢谢
您正在检查downpaymentval
,但该变量不在您显示的代码中。应该是value
吗?
您还使用了一个点来连接字符串而不是加法符号。
尝试以下内容:
$('#downpayment').on('keyup keydown', function (
var $this = $(this), status,
GrandTotal = $("#GrandTotal").val(),
downpayment = $("#downpayment").val(),
value = GrandTotal - downpayment;
if (GrandTotal < value) {
status = "PAID OFF";
$('#status').val(status);
} else {
status = "MINUS" + value;
$('#status').val(status);
}
console.log(status);
});
您还会注意到我已经将status
变量的声明移至顶部。如果/其他语句,JavaScript不会更改内部范围,因此您不应两次声明它。
尝试解析数值为.val()给出字符串对象。
$('#downpayment').on('keyup keydown', function (
var $this = $(this),
GrandTotal = $("#GrandTotal").val(),
downpayment = $("#downpayment").val();
var value = GrandTotal - downpayment;
if (parseInt(GrandTotal) < parseInt(downpaymentval)) {
var status = "PAID OFF";
$('#status').val(status);
} else {
var status = "MINUS".value;
$('#status').val(status);
}
console.log(status);
});
您的代码上有很多错误。请参阅下面的更新代码。
<input type="number" name="GrandTotal" id="GrandTotal" class=" form-control" placeholder="Grand Total" required />
<input type="number" name="DownPayment" id="downpayment" class=" form-control" placeholder="Down Payment" required />
<input type="text" name="Status" id="status" class="form-control" placeholder="Status">
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script>
$('#downpayment').on('keyup keydown', function (){
var $this = $(this),
GrandTotal = $("#GrandTotal").val(),
downpayment = $("#downpayment").val();
var value = GrandTotal - downpayment;
var status = '';
if (GrandTotal < downpayment) {
status = "PAID OFF";
} else {
status = "MINUS" + value;
}
$('#status').val(status);
console.log(status);
});
</script>
尝试以下代码:
$('#downpayment').on('keyup keydown', function (){
var $this = $(this),
GrandTotal = parseFloat($("#GrandTotal").val()),
downpayment = parseFloat($("#downpayment").val());
var value = GrandTotal - downpayment;
var status = '';
if (GrandTotal < downpayment) {
status = "PAID OFF";
} else {
status = "MINUS " + value;
}
$('#status').val(status);
});
$('#downpayment').on('keyup', function (){
var grandTotal = $("#GrandTotal").val();
var downpayment = $("#downpayment").val();
grandTotal = grandTotal == undefined || grandTotal.length == 0?0:parseFloat(grandTotal);
downpayment = downpayment == undefined || downpayment.length == 0?0:parseFloat(downpayment);
var value = grandTotal - downpayment;
var status = value<0?"PAID OFF":"MINUS " + value;
$('#status').val(status);
});
您的代码中有很多错误。未定义的变量,您没有将值转换为整数。我在这里解决了。
https://codepen.io/anon/pen/jmeazw
$(document).ready(function() {
$('#downpayment').on('keydown', function () {
GrandTotal = $("#GrandTotal").val();
downpayment = $("#downpayment").val();
GrandTotal = parseInt(GrandTotal);
downpayment = parseInt(downpayment);
var value = parseInt(GrandTotal) - parseInt(downpayment);
var status = '';
if (GrandTotal < downpayment) {
status = "PAID OFF";
$('#status').val(status);
} else {
status = "MINUS " + value;
$('#status').val(status);
}
console.log(status);
});
});
您的代码中有一些错误。更改
1。 var status = "MINUS".value;
to var status = "MINUS" + value;
。
2。 $('#downpayment').on('keyup keydown', function (
to $('#downpayment').on('keyup keydown', function () {
更新的代码
$('#downpayment').on('change', function() {
var GrandTotal = $("#GrandTotal").val(),
downpayment = $("#downpayment").val();
var value = GrandTotal - downpayment;
if (GrandTotal <= downpayment) {
var status = "PAID OFF";
$('#status').val(status);
} else {
var status = "MINUS " + value;
$('#status').val(status);
}
console.log(GrandTotal,downpayment,status);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" name="GrandTotal" id="GrandTotal" class=" form-control" placeholder="Grand Total" required />
<input type="text" name="DownPayment" id="downpayment" class=" form-control" placeholder="Down Payment" required />
<input type="text" name="Status" id="status" class="form-control" placeholder="Status">