JQuery Hide Option在IE和Safari中不起作用

Jam*_*Lin 17 safari jquery internet-explorer option

我试图使用.hide()在下拉框中隐藏一些选项.这在firefox和chrome中运行得非常好,但它在IE和Safari中不起作用.我的原始代码更复杂,但我已将其缩小到这个范围.

我尝试了几种组合,没有任何效果.

.hide()有效,但出于某种原因不适用于选项标签内的内容.

有人可以帮帮我吗?这让我疯了.非常感谢您抽出时间的帮助!

这是我的jscript:

    <script type="text/javascript" src="js/jquery-1.7.1.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
            $(".wrapper1").hide();
        });
    </script>
Run Code Online (Sandbox Code Playgroud)

这是HTML:

                <label for="prodName">Product Name:</label> 
                <input type="text" name="prodName" /><br />

                <label for="candy">Candy:</label> 
                <select name="candy" id="candy">
                        <option value="0" class="blank" selected="selected"></option><!-- PHP and JS validators should not allow "0" here. User should be prompted to select something. -->
                        <option value="1" class="wrapper1">Hide this 1</option>
                        <option value="2" class="wrapper1">Hide this 2</option>
                        <option value="3" class="wrapper2">Show this 1</option>     
                </select><br />
Run Code Online (Sandbox Code Playgroud)

Joh*_*ith 11

这将有效..改变.show到.showOption和.hideOption.然而,这在IE中仍然很糟糕,因为在Firefox中你可以让它隐藏一个被选中的选项.因此,如果显示"选择一个"并被隐藏.Firefox仍然会说"选择一个".

$.fn.showOption = function() {
this.each(function() {
    if( this.tagName == "OPTION" ) {
        var opt = this;
        if( $(this).parent().get(0).tagName == "SPAN" ) {
            var span = $(this).parent().get(0);
            $(span).replaceWith(opt);
            $(span).remove();
        }
        opt.disabled = false;
        $(opt).show();
    }
});
return this;
}
$.fn.hideOption = function() {
this.each(function() {
    if( this.tagName == "OPTION" ) {
        var opt = this;
        if( $(this).parent().get(0).tagName == "SPAN" ) {
            var span = $(this).parent().get(0);
            $(span).hide();
        } else {
            $(opt).wrap("span").hide();
        }
        opt.disabled = true;
    }
});
return this;
}
Run Code Online (Sandbox Code Playgroud)


Rig*_*red 8

你是对的.有些浏览器不会让你隐藏option元素.您可能需要删除它们.

虽然可能更好(或至少是替代)的可能性是禁用它们.

$(".wrapper1").prop('disbled', true);
Run Code Online (Sandbox Code Playgroud)


Jim*_*ose 5

您必须删除option元素.display:none许多浏览器都不支持隐藏它们.

隐藏

var elems = $(".wrapper1").remove();
Run Code Online (Sandbox Code Playgroud)

节目

$('#candy').append(elems);
Run Code Online (Sandbox Code Playgroud)