2015-01-05 122 views
1

我有一個小盒子,在那裏我可以改變語言,但它不工作。我沒有看到任何錯誤。如果我點擊此框不開放,我不能選擇該選項。但爲什麼?看不到選項,不能選擇

我想要做什麼?我想要做這樣的事情:如果妳選擇選項 - >然後EST它去index.html,然後如果u選擇選項 - >然後ENG它去index_eng.html

在HTML我有:

<nav id="menu4"> 
    <select> 
     <option href="index.html" value="est">EST</option> 
     <option href="index_eng.html" value="eng">ENG</option> 
    </select> 
</nav> 

在CSS我:

#menu4 { 
    position: absolute; 
    right: 0px; 
} 

#menu4 ul { 
    text-align: right; 
} 

#menu4 a { 
    color: #FFF; 
    font-weight: normal; 
    line-height: 22px; 
    font-size: 16px; 
    font-family: 'futura_bk_btbook'; 
} 

#menu4 .selected a { 
    /*text-decoration: underline;*/ 
} 

而且我也有劇本爲這個下拉菜單:

$('option').click(function() { 
    var url = $(this).attr('href'); 
    window.location = url; 
}); 
+1

''點擊''

+1

您無法將事件綁定到選項標記,IE是一個不會註冊它們的瀏覽器。在選擇時使用更改處理程序 – charlietfl

+0

但是我怎樣才能綁定事件呢? – frantsium

回答

3

你可以試試這個

<select name="menu4" onchange="window.location = this.options[this.selectedIndex].value;"> 
<option value="index.html">EST</option> 
<option value="index_eng.html">ENG</option> 
</select> 
+0

這看起來不錯,但它爲什麼不在我的頁面中打開?如果我在wamp中嘗試它,但是如果我嘗試只是'file:/// C:/ Users/Erki/Desktop/asdasdasd/homepage/index.html',那麼這個選擇不會打開。 – frantsium

+0

我想它不允許從JavaScript加載本地資源。 – chridam

-2

呦您應該將change事件綁定到SELECT而不是OPTION標籤。每次選擇新選項時,將觸發事件onchange

jQuery('select').on('change', function() { 
    var url = jQuery(this.options[this.selectedIndex]).attr('href'); 
    window.location = url; 
}); 
+1

是的,但這個選擇不開放,爲什麼? – frantsium

-2

HTML

<nav id="menu4"> 
    <select> 
     <option value="index.html" >EST</option> 
     <option value="index_eng.html">ENG</option> 
    </select> 
</nav> 

JS代碼

$('select').on('change',function(){ 
    var url = $(this).val(); 
    window.location = url; 
}); 
+0

嘗試這個跨瀏覽器,它會在一些失敗。任何對事件的支持都是非標準的 – charlietfl

0

您需要我們change上的選擇,然後獲取相關optionhref

$('select').on('change',function() { 
    var href = $(this).children('option:selected').attr('href'); 
    if (href) { 
     window.location = href; 
    } 
}); 

另外,還要注意option元素沒有一個href屬性。如果要將任意信息放在元素上,請使用data-* attributes

例(使兩者的變化):

$('select').on('change',function() { 
 
    var href = $(this).children('option:selected').attr('data-href'); 
 
    if (href) { 
 
    alert("Changed: " + href); 
 
    } 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<select> 
 
    <option data-href="index.html" value="est">EST</option> 
 
    <option data-href="index_eng.html" value="eng">ENG</option> 
 
</select>

0

剛剛嘗試這一點。

<nav id="menu4"> 
    <select id="swt_lng"> 
     <option value="#" >Language</option>  
     <option value="index.html" >EST</option> 
     <option value="index_eng.html" >ENG</option> 
    </select> 
</nav> 

$('#swt_lng').change(function(){ 
    var url = $(this).val(); 
    window.location = url; 
}); 

<nav id="menu4"> 
     <select> 
      <option value="#" >Language</option> 
      <option value="index.html" >EST</option> 
      <option value="index_eng.html" >ENG</option> 
     </select> 
    </nav> 

    $('#menu4 select').change(function(){ 
     var url = $(this).val(); 
     window.location = url; 
    }); 
+0

由於select元素本身沒有值 - 這真的在jQuery中有效嗎? – feeela

+0

是的,它會工作 –

+1

@FarveenHassan是的,它的工作原理。閱讀相同的jQuery文檔 – amit

1

您可以使用.change(function() {...});做到這一點。

Here是演示。

$("select").change(function(){ 
var url = $(this).children('option').attr('href'); 
window.location = url; 
});