在jQuery中的逻辑条件(logical condition in jquery)

编程入门 行业动态 更新时间:2024-10-26 06:30:20
在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"/> 
  
 

更多推荐

本文发布于:2023-08-06 16:26:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1454023.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:逻辑   条件   jQuery   jquery   condition

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!