How to hide html form select option?

后端 未结 4 1061
萌比男神i
萌比男神i 2021-01-24 01:43

I have a very simple form which I would like to hide select options when another select field refines it.

Currently I have tried dispay: none as an inline s

相关标签:
4条回答
  • 2021-01-24 02:05

    I think the easier way to accomplish what you have in mind is to have three different dropdown lists in place of the second dropdown. Then, the first drop down can choose which one of those to show based on it's value.

    Much simpler.

    With that change, you can make your callback simply:

    $('#refine').change(function () {
        var refine = $('option:selected', this).val().replace(/ /g, "-");
    
        $('.refined').removeClass('hidden').addClass('hidden');
        $('#'+refine).removeClass('hidden').removeAttr('disabled');
    });
    

    Edit: I forked your fiddle (and maybe edited your original?). It has the changes I suggested.

    0 讨论(0)
  • 2021-01-24 02:17

    In the end I used jquery clone, which seemed to do the trick.

    http://jsfiddle.net/3vPgY/10/

    var everything = $('#everything').clone(true);
    
    $('#refine').change(function () {
    
        var selectColour = $('option:selected', this).val().replace(/ /g, "-");
    
        if (refine != 0) {
    
            var everythingRefined = everything.clone(true).find('.default,.'+selectColour);
    
            $('#everything').removeAttr('disabled');
            $('#everything').empty().append(everythingRefined);
    
        } else {
    
            $('#everything').attr('disabled', 'disabled');
    
        }
    
    });
    
    0 讨论(0)
  • 2021-01-24 02:19

    This is not the best way to do things, it's best to dynamically build your selects. HOWEVER, if you must do it this way, use the following code:

        $('#refine').change(function () {
    
            var selected = $(this).val();
            $("#everything option").wrap("<span style='display:none' />");
            $("#everything option." + selected).unwrap();
            $("#everything").attr('disabled',false);
    
    });
    

    I forked it here: http://jsfiddle.net/AyX9Q/

    Bear in mind, the code is minimal, just to show you how to wrap options in spans and hide them. It doesn't do any logic to show all options if you revert to the "please select" on the first select.

    0 讨论(0)
  • 2021-01-24 02:20

    You cannot hide/remove individual options, only apply the attribute disabled on them. They will appear grey'ed out but won't be selectable.

    You could progmatically keep a variable object that has all the data for the default configuration of the select. Then when you need to remove options you can but still have a way to know what needs to go into the select when all your conditions align.

    0 讨论(0)
提交回复
热议问题