简体   繁体   English

下拉列表和按钮不起作用

[英]Drop down list and button not working

I have the HTML segment: 我有HTML部分:

<select id="myList">
<option value="1"selected="selected">Position 1</option>
<option value="2">Position 2</option>
<option value="3">Position 3</option>
<option value="4">Position 4</option>
<option value="5">Position 5</option>
</select>
<input type="submit" id="go" value="Go to Position"></input>

Which prompts the user to select a position for a spotlight. 提示用户选择聚光灯的位置。 The JS side contains this: JS端包含以下内容:

var select = document.getElementById("myList");
var answer = select.options[select.selectedIndex].value;

if (answer == "1") {
    document.getElementById("go").onclick = function () { ightAtX = -1.0; lightAtZ = 0.5; };
} else if (answer == "2") {
    document.getElementById("go").onclick = function () { lightAtX = -1.0; lightAtZ = -0.5; };
} else if (answer == "3") {
    document.getElementById("go").onclick = function () { lightAtX = 0.0; lightAtZ = -0.5; };
} else if (answer == "4") {
    document.getElementById("go").onclick = function () { lightAtX = 1.0; lightAtZ = -0.5; };
} else if (answer == "5") {
    document.getElementById("go").onclick = function () { lightAtX = 1.0; lightAtZ = 0.5; };
} else {
    return; 
}

I want the user to click the 'Go to Position' button in order to change the position of the spotlight. 我希望用户单击“转到位置”按钮以更改聚光灯的位置。 However, when I click the button, nothing changes. 但是,当我单击按钮时,没有任何变化。 What am I missing? 我想念什么?

You need to check the selection (answer) inside of the onclick function. 您需要检查onclick函数内部的选择(答案)。 The answer is evaluated once (unless there's more code not included), and wont be dynamically updated unless you tell it to. 答案将被评估一次(除非不包括更多代码),除非您告知,否则不会动态更新。

Something like this: 像这样:

document.getElementById("go").onclick = function () { 
    var select = document.getElementById("myList");
    var answer = select.options[select.selectedIndex].value;

   if (answer == "1") {
   }...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM