2016-01-28 29 views
0

我想在頁面加載時使用顯示selectbox選項。我已經使用.simulate().trigger函數,但它不工作,所以你可以請建議一些解決方案。使用jquery打開selectbox選項

代碼:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script> 
<script src="simulate.js"></script> 
<script> 
$(document).ready(function(){ 
    $("#test").simulate('mousedown'); // not working 
    $("#test").trigger('click'); // not working 
    //$("#test").attr('size',3); // size is not as i need. i want to display option as any dropdown shows. 
}); 
</script> 

<select id="test"> 
    <option value="0">select option</option> 
    <option value="1">1</option> 
    <option value="2">2</option> 
    <option value="3">3</option> 
</select> 

請幫我出從這個問題...

+2

可能重複:http://stackoverflow.com/questions/430237/is-it-possible-to-use-js-to-open-an-html-select-以顯示其選項列表 – ste2425

回答

0

它無法打開使用JavaScript一個選擇框。 我試過了,很失望。我終於改變了屬性大小的選擇框,以便它似乎是開放的:

$('#test').attr('size',3); 

,然後當你完成

$('#test').attr('size',0); 

希望幫助:)

+0

感謝您的幫助,但根據我在我的代碼中評論我不需要.attr('size',0)這不是按照我的要求。它看起來像多選。 – Jalpesh

+0

哦,我錯過了你的內嵌評論。你介意使用庫嗎? https://select2.github.io是一個簡單的jQuery選擇框替換。我認爲它會適合你! –

0

試試這個。 ..

$(document).ready(function(){ 

    var myDropDown=$("#test"); 
    var length = $('#test> option').length; 
    myDropDown.attr('size',length); 
    $("#test").click(function(){ 
     myDropDown.attr('size',0); 
    }); 

}); 
+0

Plunker演示鏈接:https://plnkr.co/edit/jHGlyiuXFVjdByxgSNTY?p=preview – Nofi

+0

感謝您的幫助,但根據我在我的代碼中評論我不需要.attr('size',0)這不是根據我的要求。它看起來像多選。 – Jalpesh

+0

哦,我錯過了你的內嵌評論...對不起... ...順便說一句,我沒有得到重點「它看起來像多選擇」...如果你運行這個代碼只有單選擇下拉列表填充正確。 – Nofi

相關問題