首页 > 解决方案 > 使用列表更改 html 页面白色事件 onchange

问题描述

我的 index.html 上有这个列表

<select id="selectFaq">
    <option selected>Selecciona un FAQ</option>
    <option value="JavaFAQ.html">Java FAQ</option>
    <option value="netFAQ.html">.Net FAQ</option>
    <option value="PhpFAQ.html">PHP FAQ</option>
</select>

当用户更改列表中的默认值时,下一个代码的提议会更改当前页面:

window.onload = configuraSelect;

function configuraSelect() {
    document.getElementById("selectFaq").selectedIndex = 0;
    document.getElementById("selectFaq").onchange = cambiaPagina;
}

function cambiaPagina() {
    var elementoSelect = document.getElementById("selectFAQ");
    var nuevaPagina = elementoSelect.options[elementoSelect.selectedIndex].value;
    if (nuevaPagina != "") {
        window.location = nuevaPagina;
    }
}

但是,关于句子

var nuevaPagina = elementoSelect.options[elementoSelect.selectedIndex].value;

代码没有返回任何值,我该怎么办?

标签: javascripthtml

解决方案


会不会是拼写错误?

而不是这个:

var elementoSelect = document.getElementById("selectFAQ");

尝试这个:

var elementoSelect = document.getElementById("selectFaq");

推荐阅读