我认为这很容易,但我无法让它工作。无法在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>
但它不工作。
也许知道我在做什么错了?
你如果和else语句不正确。 “=”用于赋值,如果你想比较你使用“==”的东西 – 2017-01-09 16:39:02
你有另一个问题:[为什么我的平等比较使用=(一个等于)工作正常?](http:// stackoverflow.com/q/38268329/218196) –
*“但它不起作用。”*您能详细说明吗?发生了什么以及你期望发生什么?请阅读[问]一个问题。 –