在链接 下拉选项(Links in <select> dropdown options

2019-06-18 01:05发布

是否有可能时,无需外部按钮选择的每个选项下拉菜单中的某个地方联系起来?

<select>
  <option value="x">x</option>
  <option value="y">y</option>
</select>

Answer 1:

您可以使用onChange属性中。 就像是:

<select onChange="window.location.href=this.value">
    <option value="www.google.com">A</option>
    <option value="www.aol.com">B</option>
</select>


Answer 2:

添加onchange事件处理程序和页面位置的值设置为

<select id="foo">
    <option value="">Pick a site</option>
    <option value="http://www.google.com">x</option>
    <option value="http://www.yahoo.com">y</option>
</select>

<script>
    document.getElementById("foo").onchange = function() {
        if (this.selectedIndex!==0) {
            window.location.href = this.value;
        }        
    };
</script>


Answer 3:

...或者,如果你想/需要保持你的选择“值”,因为它是,只需添加一个新的属性:

<select id="my_selection">
<option value="x" href="/link/to/somewhere">value 1</option>
<option value="y" href="/link/to/somewhere/else">value 2</option>
</select>

<script>
document.getElementById('my_selection').onchange = function() {
    window.location.href = this.children[this.selectedIndex].getAttribute('href');
}
</script>


Answer 4:

您可以使用此代码:

<select id="menu" name="links" size="1" onchange="window.location.href=this.value;">
    <option value="URL">Book</option>
    <option value="URL">Pen</option>
    <option value="URL">Read</option>
    <option value="URL">Apple</option>
</select>


文章来源: Links in 举报