简体   繁体   English

使用jQuery UI selectmenu选择中的隐藏/显示选项

[英]hide/show option in select using jQuery UI selectmenu

I am using the jquery selectmenu plugin to display a nicely style select menu. 我正在使用jquery selectmenu插件显示漂亮的样式选择菜单。 I have three select menus, all options are hidden in the last two menus, when you select an option from menu one it shows the relevant options in menu two. 我有三个选择菜单,所有选项都隐藏在最后两个菜单中,当您从菜单一中选择一个选项时,它将显示菜单二中的相关选项。 The same thing happens for menu the two to three. 同样的事情发生在菜单二到三上。 Without the selectmenu plugin this works fine. 如果没有selectmenu插件,则可以正常工作。 Unfotunately with the select menu plugin it disapears and doesn't reappear. 不幸的是,它与选择菜单插件一起消失了,并且没有重新出现。

DEMO http://jsfiddle.net/GXtpC/1525/ 演示http://jsfiddle.net/GXtpC/1525/

$(function(){            
    $('select').selectmenu({
        style:'popup'
    });

    $('.select1').on('change', function () {
        var parentId = $(this).children(":selected").attr("id");
        $('.all-opt').hide();
        $('.' + parentId).show();
    });
    $('.select2').on('change', function () {
        var parentIdReg = $(this).children(":selected").attr("data-id");
        $('.select3-option').hide();
        $('.' + parentIdReg).show();
    });

}); 

Fully working without jQuery selectmenu plugin 无需jQuery selectmenu插件即可完全工作

DEMO http://jsfiddle.net/GXtpC/1526/ 演示http://jsfiddle.net/GXtpC/1526/

I'm not familiar with the selectmenu() widget, but I can point point to some things to note. 我对selectmenu()小部件不熟悉,但是我可以指出一些要注意的地方。 Modifying your code a little, it seems like after calling the selectmenu() , it makes calling hide() not possible: 稍微修改一下代码,似乎在调用selectmenu() ,就不可能调用hide()

$(function(){            
    $('.select1').selectmenu();
    $('.select3').selectmenu();

    $('.select2').hide();    <---- hidden
    $('.select3').hide();    <---- not hidden
/*
    $('.select1').on('change', function () {
       ...
       ...
    */
});  

Also, a slight typo in the form caused 此外,形式上的轻微错别字也会引起

SELECT OPTION 1
SELECT OPTION 1
SELECT OPTION 3

I suppose it should be 1,2 and 3? 我想应该是1,2和3?

Use the following code: 使用以下代码:

// init
var mySelect = $( "#my-select" ).selectmenu();

// hide
mySelect.selectmenu('close');
mySelect.widget().hide();

// show
mySelect.widget().show();

I had similar problem, which I solved calling 我有类似的问题,我解决了打电话

$( "#original_select_id" ).selectmenu( "refresh" );

I had code which removed or appended select options based on selection of other select. 我有基于其他select的选择删除或附加了select选项的代码。 IE have problems with show/hide method for select options, that is why I use remove/append method instead (maybe this is required for the provided code to solve this problem). IE的select选项的show / hide方法有问题,这就是为什么我改用remove / append方法(也许这是所提供的代码解决此问题所必需的)的原因。

With this code executing after the changes in the original select element was done, the jQuery UI selectmenu "reinitialized" itself and contains only options that was added / not removed on the fly. 在完成原始选择元素的更改后执行此代码后,jQuery UI selectmenu自身会“重新初始化”,并且仅包含动态添加或未删除的选项。

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

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