条件检查是间歇性的

时间:2016-12-20 07:29:18

标签: javascript jquery

功能性:

用户必须在receipt_details页面输入他们的支出,并且根据他们是否选中了复选框,最低支出条件会有所不同。如果他们已经检查过,最低支出为120美元,否则为150美元。

页面中有2个收据明细文本字段,因此,这些将是以下检查条件:

1。)if(receipt_field_1大于120或者receipt_field_2大于120或者receipt_field_1& receipt_field_2的总和大于120) - >用户可以提交并导航到下一页。否则,将出现错误消息

2。)if(receipt_field_1大于150或者receipt_field_2大于150或者receipt_field_1& receipt_field_2的总和大于150) - >用户可以提交并导航到下一页。否则,将出现错误消息

问题:

此时,检查条件为 CORRECT 一致,表现为以下行为:

1。)当receipt_field_1或receipt_field_2超过规定值(120/150)时,它将允许用户提交并导航到下一页,错误信息

我遇到的问题是最终检查SUM条件不一致:意思是,有时它能够解密并计算总和是否小于或大于规定值(120/150),有时如果总和小于或大于

,则无法解密和计算

因此,我不确定为什么检查SUM条件如此不一致会如此。请帮忙。



 //AmexCard User
 if ($('#AmaxCardField').is(':checked')) {

   //Check that the input value field is $120 or more else, inform that minimum spending is 120
   if (($("#ReceiptField_1").val() >= 120) || ($("#ReceiptField_2").val() >= 120) || ((($("#ReceiptField_1").val()) + ($("#ReceiptField_2").val())) >= 120)) {

     //Condition Passed
     console.log("Amex user and spent more than 120");

     alert("You are an AMEX member and spent more than 120");
   } else {
     //inform that minimum spending is 120

     alert("You need to spend more than 120");
   }

 } else if ((!$('#AmaxCardField:checked').length)) {

   //Check that the input value field is SGD$150 or more else, inform that minimum spending is SGD150
   if (($("#ReceiptField_1").val() >= 150) || ($("#ReceiptField_2").val() >= 150) || ((($("#ReceiptField_1").val()) + ($("#ReceiptField_2").val())) >= 150)) {

     //Condition Passed
     console.log("Non-Amex user and spent more than SGD150");

     alert("You are an AMEX member and spent more than 150");
   } else {
     //inform that minimum spending is SGD150
     alert("You need to spend more than 120");

   }
 }

<form>

  <!-- DropDown Menu to choose Participating Outlet -->
  <select id="dropDownShops_1">
    <option value="" selected disabled>Please Select Shops ...</option>
  </select>

  <input type="text" id="ReceiptField_1" style="z-index=10; position:absolute; top:390px; left:858px; height:58px; width:265px; outline:0; border: 0; font-size:25px; font-family:'Gothic'; color:#765725; background: transparent;" autofocus>

  <select id="dropDownShops_2">
    <option value="" selected disabled>Please Select Shops ...</option>
  </select>

  <input type="text" id="ReceiptField_2" style="z-index=10; position:absolute; top:585px; left:858px; height:58px; width:265px; outline:0; border: 0; font-size:25px; font-family:'Gothic'; color:#765725;  background: transparent;">

  <input type="checkbox" id="AmaxCardField" style="z-index=10; position:absolute; top:690px; left:420px; height:30px; width:30px; outline=0; border: 0; background: transparent;">
</form>
&#13;
&#13;
&#13;

PLunker:https://plnkr.co/edit/obkHLkBC7toFo4t30Sfd?p=catalogue

2 个答案:

答案 0 :(得分:1)

将字符串解析为数字

parseInt($("#ReceiptField_1").val()) + parseInt($("#ReceiptField_2").val()) >= 150

注意:您可能想要进行一些选择器缓存

答案 1 :(得分:1)

来自文本字段的任何值始终是字符串。

所以

$("#ReceiptField_1").val()

$("#ReceiptField_2").val()
即使你输入数字,

也会返回一个字符串。

这意味着对于任何数值运算,首先必须将它们转换为整数,如

var rf1 = parseInt($("#ReceiptField_1").val())

你的病情会改为

var rf1 = parseInt($("#ReceiptField_1").val());
var rf2 = parseInt($("#ReceiptField_2").val());

if((rf1 >= 150 || rf2 >= 150) || ((rf1+rf2)>=150))

现在,

为什么它适用于

$("#ReceiptField_1").val() >= 120
$("#ReceiptField_2").val() >= 120

因为没有超过该值的操作。将按原样比较值。

但是如果你添加2个值,它将是字符串连接而不是添加,因为它们都是字符串而不是数字。