jQuery如果条件不能正常工作

时间:2018-01-30 07:15:35

标签: javascript jquery html html5 if-statement

我想在代码中做的是,我将计算所有字段的总和(输入类型编号字段)。总结之后,我将用if / else语句比较它们​​的不同范围。我的代码无效。

HTML CODE

<p class="line-item-property__field">
   <label for="B1">B1 (Min. 50)</label>
   <input required="" id="B1" style="width:120px; max-width:100%;" value="0" min="0" step="50" name="properties[B1]" type="number" class="inp">
</p>
<p class="line-item-property__field">
   <label for="B2">B2 (Min. 50)</label>
   <input id="B2" style="width:120px; max-width:100%;" value="0" min="0" step="50" name="properties[B2]" type="number" class="inp">
</p>
<p class="line-item-property__field">
   <label for="B3">B3 (Min. 50)</label>
   <input id="B3" style="width:120px; max-width:100%;" value="0" min="0" step="50" name="properties[B3]" type="number" class="inp">
</p>
<p class="line-item-property__field">
   <label for="B4">B4 (Min. 50)</label>
   <input id="B4" style="width:120px; max-width:100%;" value="0" min="0" step="50" name="properties[B4]" type="number" class="inp">
</p>
<p class="line-item-property__field">
   <label for="B5">B5 (Min. 50)</label>
   <input id="B5" style="width:120px; max-width:100%;" value="0" min="0" step="50" name="properties[B5]" type="number" class="inp">
</p>
<p class="line-item-property__field">
   <label for="B6">B6 (Min. 50)</label>
   <input id="B6" style="width:120px; max-width:100%;" value="0" min="0" step="50" name="properties[B6]" type="number" class="inp">
</p>

我的jQuery代码

$(".inp").change(function(){
   var sum = 0;
   $(".inp").each(function(){
       sum += parseInt($(this).val());
   });

   if(sum => 50 && sum < 100){
       alert("in between range");
   }
   else{
       alert("Out of the range");
   }
});

注意:总和已正确计算,但问题是if条件无效。

1 个答案:

答案 0 :(得分:1)

=>需要>=

如下所示: -

if(sum >= 50 && sum < 100){
  alert("in between range");
}