2016-12-20 32 views
1

功能:條件檢查斷續進行

的用戶在receipt_details頁面輸入自己的開支,並根據他們是否已經檢查一個複選框,最小的支出條件將differ.If他們已經檢查中,最低支出是120美元,否則就是150美元。

有2個收據細節的TextField在頁面因此,這些將是下面的檢查條件:

1)如果(receipt_field_1超過120 OR receipt_field_2超過120 OR receipt_field_1的總和& receipt_field_2是超過120) - >用戶可以提交併導航到下一頁。否則,一個錯誤味精將出現

2.)如(receipt_field_1超過150 OR receipt_field_2超過150 OR receipt_field_1 & receipt_field_2的總和超過150) - >用戶可以提交併導航到下一頁。否則,一個錯誤味精會出現

問題:

在這一點上,檢驗條件是正確一致的以下行爲:

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>

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

回答

1

來自何處的任何值文本字段是al方式一個字符串。

所以

$("#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個值,它將是字符串連接而不是添加,因爲它們都是字符串而不是數字。

+0

非常感謝!!詳細的解釋 – Luke

1

解析你的字符串編號

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

注意:您可能需要做一些選擇緩存