How to remove/hide select options from select-list

前端 未结 11 2472
南笙
南笙 2021-02-20 07:53

I\'ve got a select list like this:


                        
    
提交评论

  • 2021-02-20 08:02

    To hide it, wrap it with a span tag.

    $( "#selectlist option[value=4]" ).wrap( "<span>" );
    

    To show it, unwrap the span tag if it has one.

    if ( $( "#selectlist option[value=4]" ).parent().is( "span" ) ){
        $( "#selectlist option[value=4]" ).unwrap();
    }
    
    0 讨论(0)
  • 2021-02-20 08:09

    You can hide option using following line include in scripting.

        $("#selectlist option[value='4']").hide();
    

    And if you want to again show this option use following line.

       $("#selectlist option[value='4']").show();
    
    0 讨论(0)
  • 2021-02-20 08:10
    <option value="4" style="display:none">Product 4</option> 
    

    hides the 4th option by default.

    0 讨论(0)
  • 2021-02-20 08:13

    using css to hide options is not supported in IE, so you need to update the options list itself.

    Try something like

    $('#selectlist option[value=4]').remove();
    

    Demo: Fiddle

    or if you want to enable it later

    var sel = $('#selectlist');
    var opts = sel.find('option');
    
    $(':checkbox').click(function(){
        sel.empty().append(this.checked ? opts : opts.filter('[value!=4]'));
    }).click()
    

    Demo: Fiddle

    0 讨论(0)
  • 2021-02-20 08:14

    I suppose you are using JQuery as well.

    $("#selectlist option[value='option4']").remove();
    
    0 讨论(0)
  • 提交回复
    热议问题