【问题标题】:Hide / Show select options based on another selector's option in jQuery隐藏/显示基于 jQuery 中另一个选择器选项的选择选项
【发布时间】:2011-06-13 23:10:36
【问题描述】:

有一个类似的问题在这里得到了很好的回应:jQuery disable SELECT options based on Radio selected (Need support for all browsers)

我想知道如何修改代码以使用选择框而不是单选按钮。

【问题讨论】:

    标签: javascript jquery select show-hide


    【解决方案1】:
    the modified plugin is:
    
        jQuery.fn.filterOn = function(selection, values) {
            return this.each(function() {
                var select = this;
                var options = [];
                $(select).find('option').each(function() {
                    options.push({value: $(this).val(), text: $(this).text()});
                });
                $(select).data('options', options);
                $(selection).change(function(){
                        $(selection + " option:selected").each(function(){
                            var options = $(select).empty().data('options');
                            var haystack = values[$(this).attr('id')];      
                            $.each(options, function(i){
                                var option = options[i];
                                if($.inArray(option.value, haystack) !== -1) {
                            $(select).append(
                            $('<option>').text(option.text).val(option.value)
                            );
                        }
                            });
                        });
                    });
            });
        };
    
            $(function() {
            $('#theOptions').filterOn('#dropDown', {
                'abc': ['a','b','c'],
                '123': ['1','2','3']        
            });
        });
    

    示例 HTML 是:

    <select id="dropDown">
        <option value="abc" id="abc" >ABC</option>
        <option value="123" id="123">123</option>
    </select>
    
    <select id="theOptions">
      <option value="1">1</option>
      <option value="2">2</option>
      <option value="3">3</option>
      <option value="a">a</option>
      <option value="b">b</option>
      <option value="c">c</option>
    
    </select>
    

    为了完整起见,这里是demo

    【讨论】:

    • ...还有演示?我投了赞成票,但很高兴看到一些工作的演示 =) 也许JS FiddleJS Bin
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-24
    • 1970-01-01
    • 1970-01-01
    • 2021-08-06
    • 1970-01-01
    • 2017-04-10
    相关资源
    最近更新 更多