在表单<select>元素中,如何使用onchange事件处理程序直接链接到所选选项?

2 javascript forms text widget

这是我过去用于非wordpress网站的内容:

function goThere() {
    var list = document.forms[0].articles
    location = list.options[list.selectedIndex].value
}
Run Code Online (Sandbox Code Playgroud)

调用该函数的select元素:

<form>
<select id="articles" name="articles" onchange="goThere()">
 <option value="#" selected>Choose an article</option>
 <option value="document1.pdf">Document 1</option>
 <option value="document2.pdf">Document 2</option>
 <option value="document3.pdf">Document 3</option>
</form>
Run Code Online (Sandbox Code Playgroud)

bwe*_*est 5

您没有关闭您的选择标记.

你没有用分号结束你的javascript行.

location不是对象,需要使用document.location.href.

试试这个:

function goThere() {
    var list = document.getElementById('articles');
    document.location.href = list.options[list.selectedIndex].value;
}
Run Code Online (Sandbox Code Playgroud)