我有这段代码来比较两个值以验证它们是否相同:
$(document).on("blur", "[id$=boxSection5Total]", function (e) {
var totalvalue = $(this).val();
var paymenttotalvalue = $('[id$=boxPaymentAmount]').val();
if (totalvalue != paymenttotalvalue) {
console.log("The value in 'Total' does not equal the previous value in 'Payment Total.'");
alert("The value in 'Total' does NOT equal the previous value in 'Payment Total.' payment total is " + paymenttotalvalue + " and total is " + totalvalue);
}
else {
console.log("The value in 'Total' DOES equal the previous value in 'Payment Total'");
}
});
但是,如果两个文本元素都留空,则失败 - 它们被视为不相等(“if (totalvalue != paymenttotalvalue)”条件为真)。
如何重构代码以使其忽略两个元素都留空的情况?
就像是:
$(document).on("blur", "[id$=boxSection5Total]", function (e) {
var totalvalue = $(this).val();
var paymenttotalvalue = $('[id$=boxPaymentAmount]').val();
if ((totalvalue == null) & (paymenttotalvalue == null)) {
return;
}
. . .
});
?
“boxSection5Total”和“boxPaymentAmount”都是文本元素(文本框)。