我试图获取选择的输入值以在div onchange中显示,但它似乎不起作用,我只是想知道有人可以提供帮助还是知道原因?
function getData(dropdown) {
var values = dropdown.options[dropdown.selectedIndex].value;
var pop = Game.currentGame.ui.options.servers.values.population;
var servers = document.getElementsByClassName('hud-intro-guide')[0];
servers.textContent = "Server " + pop + " population"
}
<select class="hud-intro-server" onchange="getData(this);">
<optgroup label="asia servers">
<option value="v8617903">asia #1 </option>
<option value="v8617895">asia #2 </option>
<option value="v8617899">asia #3 </option>
<option value="v8617900">asia #4 </option>
<option value="v8617898">asia #5 </option>
<option value="v8617894">asia #6 </option>
<option value="v8617901">asia #7 </option>
<option value="v8617902">asia #8 </option>
<option value="v8617897">asia #9 </option>
</optgroup>
</select>
<div class="hud-intro-guide"> </div>
答案 0 :(得分:1)
var pop = Game.currentGame.ui.options.servers.values.population;
应该是
var pop = Game.currentGame.ui.options.servers[values].population;
答案 1 :(得分:1)
可能您的问题与您如何尝试使用变量values
有关。
var pop = Game.currentGame.ui.options.servers.values.population;
^
|
+-- Here you're accessing the attribute values
rather than getting the server related
to the chosen option.
您真正想要的是以下内容:
function getData(dropdown) {
var values = dropdown.options[dropdown.selectedIndex].value;
var pop = Game.currentGame.ui.options.servers[values].population;
^
|
+-- With this, you're getting the server.
var servers = document.getElementsByClassName('hud-intro-guide')[0];
servers.textContent = "Server " + pop + " population"
}
答案 2 :(得分:-3)
您不需要在选择标签上添加更改事件。只需在document的jquery函数中的select上添加change事件。readyevent编写如下:
$('#your select id').on('change', function() { alert( this.value );});
OR
$('.your select class').on('change', function() { alert( this.value );});