2013-03-13 30 views
1

當用戶選擇一個答案,下面的代碼應該產生不同的結果;然而,它只是給出了相同的結果。爲什麼按下按鈕時會得到相同的結果?如果函數返回相同的輸出

1.Is the network problem affecting just your pc? <BR> 

<select id="Area"> 

    <option value="Yes">Yes</option> 

    <option value="No">No</option> 

    </select> 

<br> 

<br><p> 



2.Can you access the internet? <BR> 



<select id="Internet"> 

    <option value="Yes">Yes</option> 

    <option value="No">No</option> 

    <option value="Yes,but is slow">Yes,but is slow</option> 

    <option value="Drop in connection">Drop in connection</option> 

</select> 

<br> 

<br><p> 



3.Are you receiving any error message saying remote system could not be found or connection has timed out?<BR> 

<select id="Errors"> 

    <option value="Remote">Remote system could not be found</option> 

    <option value="Connection">Connection has timed out</option> 

    <option value="No">No error messages </option> 

    </select> 

<br> 

<br><p> 



4.Have you changed any software or Hardware before the problem occurred?<BR> 



<select id="Change"> 

    <option value="Software">Software</option> 

    <option value="Hardware">Hardware</option> 

    <option value="Both">Both</option> 

    <option value="None">None </option> 

</select> 

<br> 

<br><p> 





<button onclick="myFunction()">Detect</button> 



<p id="Solution"></p> 



<script> 

function myFunction() 

{ 

    var AreaElem = document.getElementById("Area"); 

    var Area = AreaElem.options[AreaElem.selectedIndex].value; 

    var InternetElem = document.getElementById("Internet"); 

    var Internet= InternetElem.options[InternetElem.selectedIndex].value; 
    var ErrorsElem = document.getElementById("Errors"); 

    var Errors= ErrorsElem.options[ErrorsElem.selectedIndex].value; 
    var ChangeElem = document.getElementById("Change"); 

    var Change= ChangeElem.options[ChangeElem.selectedIndex].value; 



    if (Area=="Yes"||Internet=="Yes"||Errors=="No"||Change=="No") { 
     x="check your cable is not cut or loose"; 
} else if (Area=="Yes"||Internet=="Yes"||Errors=="No"||Change=="Both") { 
     x="network connections "; 
    } else { 
     x="other problems...."; 
    } 
    document.getElementById("Solution").innerHTML=x; 
} 

</script> 
+0

我能理解編碼就像這樣一個小的一關。但是,如果你有很多的提問/回答的形式是這樣,我認爲你應該使用做這一切,例如作爲把所有的約束條件的服務器上,並用ajax返回結果的更穩健的方式。 – 2013-03-13 19:37:09

回答

3
if (Area=="Yes"||Internet=="Yes"||Errors=="No"||Change=="No") { 
     x="check your cable is not cut or loose"; 
} else if (Area=="Yes"||Internet=="Yes"||Errors=="No"||Change=="Both") { 
     x="network connections "; 
    } else { 
     x="other problems...."; 
    } 

的問題就在這裏!您正在使用「或」操作符||你應該使用「和」運營商&& 這應該解決它

if (Area=="Yes"&&Internet=="Yes"&&Errors=="No"&&Change=="No") { 
     x="check your cable is not cut or loose"; 
} else if (Area=="Yes"&&Internet=="Yes"&&Errors=="No"&&Change=="Both") { 
     x="network connections "; 
    } else { 
     x="other problems...."; 
    } 
+0

葉氏,這就是問題所在。非常感謝! :) – 2013-03-13 19:52:20

+0

很好的回答。只是一個附錄:當您使用「或」,你說,「如果_any_這些選項是真實的,請執行下列操作...」,但是當你用「和」你說,「如果_all_這些選項是真實的,請執行以下操作...「 – Herbert 2013-03-13 19:53:36

相關問題