【问题标题】:How to hide drop down options in I.E如何在 IE 中隐藏下拉选项
【发布时间】:2014-07-29 15:51:37
【问题描述】:

我正在尝试创建一个 jquery,它将过滤 IE 中下拉菜单的选择。我尝试按照JQuery Hide Option doesn't work in IE and Safari(用户kinnou)的说明进行操作,但似乎隐藏也在删除选项。 代码是:

function filterDP(element) {
    var value = $(element).val();
    var dropdown = "#dropdownIdName";

    $( dropdown+" > option").each(function() {
        var optionValue = $(this).val();
        if (navigator.appName == 'Microsoft Internet Explorer') {
            $(dropdown).find('option[value="' + optionValue + '"]').map(function () {return $(this).parent('span').length === 0 ? this : null;})
                .wrap('<span>');
        } //'Microsoft Internet Explorer'           
        if ((value == "") ||  ($(this).text().search(value) > -1) ){ // Found=>show();
            if (navigator.appName == 'Microsoft Internet Explorer') {
                $(dropdown).find('option[value="'+optionValue+'"]').unwrap().show();
            }
            else {
                $(this).show(); //all other browsers use standard .show();                      
            }
        }               
        else { // not found=> Hide
            $(dropdown).find('option[value="'+optionValue+'"]').hide();
        }
        else { 
            $(this).hide(); //all other browsers use standard .show();                      
        }
    });
}

以及激活它的代码:

aa.prepend($('<input/>', {id: 'DPFilter',
                          onkeyup: 'filterDP(this)'
                         }));

【问题讨论】:

  • 每次添加新过滤器时,您都必须重新绘制
  • 您的括号在filterDP 函数中没有正确平衡。
  • 我已经纠正了他们@Barmar。
  • 看起来您使用 .map() 的方式应该是 .filter()。
  • 那里没有任何东西可以从 DOM 中删除任何内容。它所做的只是围绕选项包装和展开一个跨度。您是否查看过 DOM 检查器以查看是否正在删除内容?

标签: jquery internet-explorer drop-down-menu show-hide


【解决方案1】:

错误是: $( 下拉+" > 选项").each(function(). 每次输入时,都会用“span”包裹一个“选项”。因此,$( dropdown+" > option") 返回的选项越来越少。 该行应更改为: $(dropdown).find("option").each(function() ...

【讨论】:

    猜你喜欢
    • 2015-09-19
    • 1970-01-01
    • 1970-01-01
    • 2013-09-23
    • 2014-02-27
    • 2013-11-16
    • 2022-08-16
    • 2021-05-06
    • 2015-06-23
    相关资源
    最近更新 更多