2013-10-30 102 views
0

我正在使用jquery下拉菜單。它幾乎已經完成,但出於某種原因,我無法獲得onChange的工作。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

什麼瀏覽器(和版本),你具有的問題是什麼? –

+0

問題是什麼? –

+1

爲了重定向瀏覽器,您需要將'location'指向'document.location'或'location.href'。 – steveukx

回答

1

使用location.href

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

說明

關鍵字location是對象。 location對象的href屬性表示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> 
+0

這是不同的瀏覽器,因爲他的代碼工作正常,我在鉻,只使用'位置'(顯然,唯一的選擇維生素)。 – Archer

+0

「window.location ='http://www.example.com'是window.location.href ='http://www.example.com'的同義詞。」如引自https://developer.mozilla.org/en-US/docs/Web/API/Window.location –

+1

這很奇怪。我也使用chrome,並且我也在Mozilla中進行了測試。到目前爲止,沒有成功。 – Blackk

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> 
+0

仍然沒有工作。不管怎麼說,還是要謝謝你。 – Blackk

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; 
}); 

Whenever a new value is assigned to the location object, a document will be loaded using the URL as if window.location.assign() had been called with the modified URL.