在jQuery中的逻辑条件(logical condition in jquery)
我的jquery函数就像这样,我想检查用户输入netWeight大于总重量的时候,我想给它提醒它模糊。 但它不起作用,我的警惕性同时降低,但我有这种情况
function checkWeight(aboj) { var row = $(aboj).parents('.itemRow'); var gWeight = row.find('.gWeight').val() != '' ? row.find('.gWeight').val() : 0; var netWeight = row.find('.netWeight').val() != '' ? row.find('.netWeight').val() : 0; if(gWeight > netWeight) { alert("Please Check Gross Weight"); } }我的html是这样的
<input type="text" class="input netWeight" name="netWeight[]" onblur=" checkWeight(this);>My jquery function is like this i want to check when user enter netWeight greater than gross weight at that i want to give alert on it's blur. But it's not working i get both weight on alert but i have with this condition
function checkWeight(aboj) { var row = $(aboj).parents('.itemRow'); var gWeight = row.find('.gWeight').val() != '' ? row.find('.gWeight').val() : 0; var netWeight = row.find('.netWeight').val() != '' ? row.find('.netWeight').val() : 0; if(gWeight > netWeight) { alert("Please Check Gross Weight"); } }my html is like this
<input type="text" class="input netWeight" name="netWeight[]" onblur=" checkWeight(this);>最满意答案
你在比较字符串。 您需要将字符串值解析为float或integer以进行数字比较:
parseInt函数 parseFloat$('body').on('click', '#compare', function () { var gross = parseFloat($('#gross').val()); var net = parseFloat($('#net').val()); if (gross>net) { alert('gross: ' + gross + ' is greater than net: ' + net); } if(net>gross) { alert('net: ' + net + ' is greater than gross: ' + gross); } })<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> Gross:<input type="text" id="gross"/> <br/> Net:<input type="text" id="net"/> <br/> <input type="button" id="compare" value="Compare"/>You're comparing strings. You need to parse the string value to float or integer to make the numeric comparison:
parseInt parseFloat$('body').on('click', '#compare', function () { var gross = parseFloat($('#gross').val()); var net = parseFloat($('#net').val()); if (gross>net) { alert('gross: ' + gross + ' is greater than net: ' + net); } if(net>gross) { alert('net: ' + net + ' is greater than gross: ' + gross); } })<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> Gross:<input type="text" id="gross"/> <br/> Net:<input type="text" id="net"/> <br/> <input type="button" id="compare" value="Compare"/>
更多推荐
发布评论