2017-01-09 30 views
0

我認爲這很容易,但我無法讓它工作。無法在JavaScript中使用此選擇功能來工作

我希望我的按鈕在選擇不同的選項時說不同的事情。

我嘗試這樣做:

<select id="selector123"> 
    <option>test</option> 
    <option>tes2</option> 
</select> 
<br><br> 
<button onclick="test()">hoi</button> 
<script> 
    var e = document.getElementById("selector123"); 
    var strUser = 
     e.options[e.selectedIndex].value; 

    function test() { 
     if (strUser = 
      "test") { 
      alert('this should a different text'); 
     } else if (strUser = "tes2") { 
      alert('this should say this text'); 
     } 
    } 
</script> 

但它不工作。

也許知道我在做什麼錯了?

+2

你如果和else語句不正確。 「=」用於賦值,如果你想比較你使用「==」的東西 – 2017-01-09 16:39:02

+0

你有另一個問題:[爲什麼我的平等比較使用=(一個等於)工作正常?](http:// stackoverflow.com/q/38268329/218196) –

+0

*「但它不起作用。」*您能詳細說明嗎?發生了什麼以及你期望發生什麼?請閱讀[問]一個問題。 –

回答

2

的問題是,你在加載時僅設置strUser的值,如果將其移動到test()函數中,則每次單擊按鈕時它都會更新。您還使用了錯誤的比較操作,應該是===

<select id="selector123"> 
 
    <option>test</option> 
 
    <option>tes2</option> 
 
</select> 
 
<br><br> 
 
<button onclick="test()">hoi</button> 
 

 
<script> 
 
var e = document.getElementById("selector123"); 
 

 
function test() { 
 
    var strUser = e.options[e.selectedIndex].value; 
 
    if (strUser === "test") { 
 
     alert('this should a different text'); 
 
    } else if (strUser === "tes2") { 
 
     alert('this should say this text'); 
 
    } 
 
} 
 
</script>

0

您需要使用「==」比較字符串,而不只是「=」,也是移動strUser的到函數內部:

var e = document.getElementById("selector123"); 
 

 
function test(){ 
 
    var strUser = e.options[e.selectedIndex].value; 
 
\t if (strUser =="test") { 
 
\t \t alert('this should a different text'); 
 
\t }else if (strUser == "tes2") { 
 
\t \t alert('this should say this text'); 
 
\t } 
 
}
<html> 
 
<body> 
 
<select id="selector123"> 
 
\t <option>test</option> 
 
\t <option>tes2</option> 
 
</select> 
 
<br><br> 
 
<button onclick="test()">hoi</button> 
 
</body> 
 
</html>

+0

單靠這一點無法解決問題。 –

0

問題:

1:使用===在if條件比較。

2:聲明單擊事件裏面的strUser的否則它總是會在頁面加載的

<html> 
 

 
<body> 
 

 
    <select id="selector123"> 
 
    <option>test</option> 
 
    <option>tes2</option> 
 
    </select> 
 
    <br> 
 
    <br> 
 
    <button onclick="test(event)">hoi</button> 
 

 
    <script> 
 
    var e = document.getElementById("selector123"); 
 
    
 

 
    function test(event) { 
 
     
 
     var strUser = e.options[e.selectedIndex].value;  
 
     if (strUser =="test") { 
 
     alert('this should a different text'); 
 
     } else if (strUser == "tes2") { 
 
     alert('this should say this text'); 
 
     } 
 
    } 
 
    </script> 
 

 
</body> 
 

 
</html>

1

你有兩個錯誤的時間選擇的值,則更換===並使用e.value(可選)。 最重要的是,從選擇框中讀取值的代碼應該在函數中!

另外要注意等號 「==」 操作符和賦值運算符 「=」

這裏是工作的代碼片段

<html> <body> 
 
    
 
    <select id="selector123"> 
 
\t <option>test</option> 
 
\t <option>tes2</option> 
 
</select><br><br> <button onclick="test()">hoi</button> 
 
    
 
<script> 
 
\t 
 
function test(){ 
 
var e = document.getElementById("selector123"); 
 
\t var strUser = e.value; 
 
    
 
    if (strUser == "test") { 
 
     alert('this should a different text'); 
 
    } 
 
else if (strUser == "tes2") { 
 
\t alert('this should say this text'); 
 
    } } </script> 
 
    
 
</body> </html>