jquery中的逻辑条件

时间:2016-02-04 15:00:23

标签: javascript 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);>

2 个答案:

答案 0 :(得分:0)

您正在比较字符串。您需要将字符串值解析为float或integer以进行数字比较:

<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"/>
{{1}}

答案 1 :(得分:0)

我对使用parseFloat

表示敬意
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(parseFloat(gWeight) < parseFloat(netWeight))
  {
    alert("Please Check Gross Weight");
  }
}