0

我正在使用 jquery 下拉菜单。它几乎完成了,但由于某种原因我无法让 onChange 工作。

<select id="cd-dropdown" class="cd-select" ONCHANGE="location = this.options[this.selectedIndex].value;" >
    <option value="-1" selected>Selecione uma categoria</option>
    <option value="1" class="icon-google-plus">Massa Muscular</option>
    <option value="2" class="icon-facebook">Resistência</option>
    <option value="http://www.google.com" class="icon-twitter" >Vitaminas</option>
    <option value="4" class="icon-github">Emagrecimento</option>
</select>
4

3 回答 3

1

使用location.href

<select id="cd-dropdown" class="cd-select"
        ONCHANGE="location.href = this.options[this.selectedIndex].value;" >
------------------^

解释

关键字location是对象。对象的href属性location表示 URL。

完整代码:

<select id="cd-dropdown" class="cd-select" ONCHANGE="location.href = this.options[this.selectedIndex].value;" >
    <option value="-1" selected>Selecione uma categoria</option>
    <option value="1" class="icon-google-plus">Massa Muscular</option>
    <option value="2" class="icon-facebook">Resistência</option>
    <option value="http://www.google.com" class="icon-twitter" >Vitaminas</option>
    <option value="4" class="icon-github">Emagrecimento</option>
</select>
于 2013-10-30T17:26:16.520 回答
1

HTML:

<select id="cd-dropdown">
    <option disabled selected>Select</option>
    <option value="http://www.facebook.com">Facebook</option>
    <option value="http://www.google.com">Google</option>
</select>

查询:

$("#cd-dropdown").on('change', function(){
    var url = $("option:selected", this).val();
    window.location = url;
});

每当为位置对象分配新值时,将使用 URL 加载文档,就像使用修改后的 URL 调用 window.location.assign() 一样。

于 2013-10-30T17:39:55.357 回答
0

我更喜欢这个更简单的代码:

onchange="location.href = this.value;"

这是选择对象

你的代码在这里:

<select id="cd-dropdown" class="cd-select" onchange="location.href = this.value;" >
    <option value="-1" selected>Selecione uma categoria</option>
    <option value="1" class="icon-google-plus">Massa Muscular</option>
    <option value="2" class="icon-facebook">Resistência</option>
    <option value="http://www.google.com" class="icon-twitter" >Vitaminas</option>
    <option value="4" class="icon-github">Emagrecimento</option>
</select>
于 2013-10-30T17:35:31.693 回答