2016-06-07 53 views
0

目前新來的JS,並創建一個工資計算器,我有4個不同的條件可以滿足,可以說a1 a2 b1 b2,每個條件有不同的支付率。當選擇小時工資率並輸入工作時間時,只需將工作時間乘以工資率,並在底部的文本框中留下感謝信息。我的問題是,該函數並沒有停止在它應該的條件下,它直到最後一條if語句。說a2是滿足的條件,它會做這個條件,但它不會停止,並繼續到最後一條if語句。我已經嘗試在每個if語句的末尾添加一個返回碼,但是這會停止發佈謝謝郵件的完成功能。任何幫助表示感謝,謝謝。如何在滿足條件時完成功能?

var intName; 
var intHours = 0, 
    intQualifyType, intAgeType; 
var curWage = 0; 
var blnContinue; 
var strMessage = ""; 
var intNumCalc = 0; 
var curTotal = 0; 


function calcWage() { 
    intName = (document.getElementById('txtName').value); 
    intQualifyType = (document.getElementById('optQualifyType').value); 
    intAgeType = (document.getElementById('optAgeType').value); 
    intHours = parseInt(document.getElementById('txtHours').value); 

    if (intQualifyType == "Code1" || intAgeType == "Years2") { 
     curWage = intHours * 1; 
    } 
    alert(curWage); 

    if (intQualifyType == "Code1" || intAgeType == "Years1") { 
     curWage = intHours * 2; 
    } 

    document.getElementById('txtTotal').value = "$" + curWage.toFixed(2); 
    curWage = parseFloat(curWage); 
    intNumCalc++; 

    blnContinue = confirm("Do you have another quote to process"); 

    if (blnContinue) { 
     clearall(); 
    } else { 
     finishUp(); 
    } 


    } //end of calwage 

function clearall() { 
    document.getElementById('txtName').value = ""; 
    document.getElementById('txtHours').value = ""; 
    document.getElementById('msgInvest').value = ""; 
    document.getElementById('txtTotal').value = ""; 
    document.getElementById('txtName').focus(); 

    } // end of clearall 

function finishUp() { 
    strMessage = "Thank you for using our new Wage Calculator " + intName + "! "; 
    strMessage = strMessage + "For your hours this week you are currently owed $" + curWage.toFixed(2); 
    document.getElementById('msgFinish').value = strMessage; 
    intNumCalc = 0 
    curTotal = 0; 
    } //end of finishUp 
+1

輸入是什麼樣的? – epascarello

+0

<選擇類型= 「文本」 ID = 「optQualifyType」> <選定= 「選擇了」 值= 「代碼1」 的選項>是 <選項值= 「代碼2」>否

是18歲或以上的你? <選擇類型= 「文本」 ID = 「optAgeType」> <選定= 「選擇了」 值= 「Years1」 選項>是 <選項值= 「Years2」>否

回答

0

@戴夫規範有一點,如果這是你正在試圖做的功能。

此代碼將接受可能真題答案每個條件爲TRUE,由於您使用的||或運營商。

if (intQualifyType == "Code1" || intAgeType == "Years2") { 
curWage = intHours * 1; 
} 

alert(curWage); 

if (intQualifyType == "Code1" || intAgeType == "Years1") { 
    curWage = intHours * 2; 
} 

我也客串你需要使用& &和運營商的過程。

可以結合的陳述。像:

if(intQualifyType == "Code1" && intAgeType == "Years2") { 
    curWage = intHours * 1; 
}else if(intQualifyType == "Code1" && intAgeType == "Years1") { 
    curWage = intHours * 2; 
} 

alert(curWage); 

也許你需要添加:

return false; 

如果你想得到合適的條件之後終止過程。

+0

你和@Dave Norm是對的,只是不得不擺弄一些其他的編碼我錯了,但你的方法工作。現在都完美了,謝謝! –

+0

不客氣@B.Cx :) –

1

我相信你的問題是在這裏:

if(intQualifyType=="Code1" || intAgeType=="Years1") 

if(intQualifyType=="Code1" || intAgeType=="Years2") 

都將通過爲您正在使用 「或」 邏輯門運行。所以這兩個表達式都是要求intQualifyType等於Code1。 我想你要找的是一個「和」邏輯門一樣的東西:

if(intQualifyType=="Code1" && intAgeType=="Years2") 
// some more stuff here 
if(intQualifyType=="Code1" && intAgeType=="Years2") 
+0

試過這沒有運氣 –