使用jQuery比较常量值和实际值



TheresholdVal(10)的值为actualVal的10(either +10 or -10)左右。超过阈值10(例如)必须显示"Selected value is more thereshold value"。如果满足条件,则必须显示"Successfully matched records"报警。我试过下面的代码,

$(document).ready(function() {
$("#idMatch").click(function() {
var theresholdVal = "10";
var actualVal = "10"; //-examples : 10, -20, 40, 10, 20
if (
parseFloat(theresholdVal) <= parseInt(actualVal) &&
parseFloat(theresholdVal) >= parseFloat(actualVal)
) {
alert("Selected value is more than thereshold value");
} else {
alert("Matched successfully");
}
});
});
<button id="idMatch">Match</button>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

如果要检查输入值在+theresholdVal和-theresholdVal之间,则使用condition作为Math.abs(parseFloat(theresholdVal)) < Math.abs(parseInt(actualVal))

试一下

$(document).ready(function() {
$("#idMatch").click(function() {
var theresholdVal = "10";
var actualVal = $("#actualValue").val() || 0; //-examples : 10, -20, 40, 10, 20
if (Math.abs(parseFloat(theresholdVal)) < Math.abs(parseInt(actualVal))) {
alert("Selected value is more than thereshold value");
} else {
alert("Matched successfully");
}
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button id="idMatch">Match</button>
<input type="text" id="actualValue" value="10" />

最新更新