简体   繁体   English

使用jQuery打开选择框选项

[英]Open selectbox option using jquery

I want to show selectbox option using when the page loads. 我想在页面加载时使用显示selectbox选项。 I have used .simulate() and .trigger function but it's not working, so can you please suggest some solution for that. 我已经使用过.simulate().trigger函数,但是它不起作用,所以请您提出一些解决方案。

Code: 码:

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

Please help me to out from this problem... 请帮助我摆脱这个问题...

It's not possible to open a selectbox with JavaScript. 无法使用JavaScript打开选择框。 I tried and got disappointed. 我尝试过并感到失望。 I finally changed the attribute size for the select box so it appears to be open: 最后,我更改了选择框的属性大小,使其似乎处于打开状态:

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

and then when you're finished 然后当你完成

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

Hope that helps :) 希望有帮助:)

Try this... 尝试这个...

$(document).ready(function(){

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

});

I think it's not possible. 我认为这是不可能的。 There's a lot of jQuery plugins out there to enhance the native select's functionality, like the Selectmenu Widget of jQuery UI which has an "open" method: 有很多jQuery插件可以增强本机选择的功能,例如jQuery UI的Selectmenu Widget,它具有“ open”方法:

http://api.jqueryui.com/selectmenu/#method-open http://api.jqueryui.com/selectmenu/#method-open

or for Twitter Bootstrap: 或针对Twitter Bootstrap:

http://silviomoreto.github.io/bootstrap-select/methods/#selectpickershow http://silviomoreto.github.io/bootstrap-select/methods/#selectpickershow

just to name some... 仅举几例...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM