我正在使用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>
答案 0 :(得分: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>
答案 1 :(得分: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>
Jquery的:
$("#cd-dropdown").on('change', function(){
var url = $("option:selected", this).val();
window.location = url;
});
答案 2 :(得分: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>