jQuery show/hide drop-down options based on another drop-down option

前端 未结 7 721
离开以前
离开以前 2020-12-20 07:45

I am trying to create a drop-down select menu that will consist of three select boxes, where the 3rd box will show/hide specific option based on an option selected in the 1s

7条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-20 08:28

    I tried in many different ways but this solution seems reasonable and I have used in my code. No plugins required simple register function with jquery object

    Solution at glance:

    (function ($) {
    
    
    $('#showOne').click(function () {
        $('#ddlNumbers').showHideDropdownOptions('3', true);
    });
    
    $('#hideOne').click(function () {
        $('#ddlNumbers').showHideDropdownOptions('3', false);
    });
    
     $.fn.showHideDropdownOptions = function(value, canShowOption) { 
    
             $(this).find('option[value="' + value + '"]').map(function () {
                return $(this).parent('span').length === 0 ? this : null;
            }).wrap('').hide();
    
            if (canShowOption) 
                $(this).find('option[value="' + value + '"]').unwrap().show();
            else 
                $(this).find('option[value="' + value + '"]').hide();
    
    }
    
    
    
    })(jQuery);
    

    Here is the complete implementation http://jsfiddle.net/8uxD7/3/

提交回复
热议问题