2017-01-09 32 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>