如何从javascript打开JQuery selectmenu插件?

时间:2013-09-26 22:43:29

标签: javascript jquery jquery-ui select-menu jquery-ui-selectmenu

selectmenuopen方法无效。

$( "#speed" ).selectmenu('open'); // doesn't work

想法?

fiddle

HTML

<button id="btn">open it</button>
<fieldset>
    <label for="speed">Select a speed</label>
    <select name="speed" id="speed">
        <option value="Slower">Slower</option>
        <option value="Slow">Slow</option>
        <option value="Medium" selected="selected">Medium</option>
        <option value="Fast">Fast</option>
        <option value="Faster">Faster</option>
    </select>
</fieldset>

JS

$(function () {
    $( "#speed" ).selectmenu();
    $('#btn').click(function(){
        $( "#speed" ).selectmenu('open'); // doesn't work
    });
});

CSS

fieldset {
    border: 0;
}
label {
    display: block;
    margin: 30px 0 0 0;
}
select {
    width: 200px;
}
.overflow {
    height: 200px;
}

1 个答案:

答案 0 :(得分:3)

我看了一下发生了什么。它打开正常,但在打开期间,它会注册一个文档点击处理程序,关闭菜单以响应菜单外的任何点击,并且此处理程序立即处理按钮上的点击。你应该能够像这样停止处理这个点击的处理程序:

$(function () {
    $( "#speed" ).selectmenu();
    $('#btn').click(function(event){
        $( "#speed" ).selectmenu('open');
        event.stopPropagation();
    });
});