jQuery: change selectable dropdown options based on radio button selection

后端 未结 3 593
日久生厌
日久生厌 2020-12-12 03:36

I have a php web form, when user selects a radio button, I want only certain options to be available in the dropdown. So for example, if I select House fro

相关标签:
3条回答
  • 2020-12-12 03:51

    Simplest way is just assign css classes to the select options and show/hide them.

    Demo: http://jsfiddle.net/9Tbpq/

    <form method="post">
        <fieldset id="Group1" name="Group1">
            <legend>Group box</legend>
            <input id="Radio1" name="Radio1" type="radio" value="House" />House<br />
            <input id="Radio1" name="Radio1" type="radio" value="Condo" />Condo<br />
            <input id="Radio1" name="Radio1" type="radio" value="Boat" />Boat<br />
        </fieldset>
        <br/><br/>
    
        <fieldset id="Group2" name="Group2">
            <legend>Group Options</legend>
            <select id="Select1" name="Select1">
                <option>Select</option>
                <option value="HO-House 1">House 1</option>
                <option value="HO-House 2">House 2</option>
                <option value="HO-House 3">House 3</option>
                <option value="CO-Condo 1">Condo 1</option>
                <option value="CO-Condo 2">Condo 2</option>
                <option value="CO-Condo 3">Condo 3</option>
                <option value="BO-Boat 1">Boat 1</option>
                <option value="BO-Boat 2">Boat 2</option>
                <option value="BO-Boat 3">Boat 3</option>
            </select>
        </fieldset>
    </form>​
    

    .

    <script>
    jQuery(function($) {
        $('input:radio').change(function(){
            var val = $('input:radio:checked').val();
            console.log(val)
            $('#Select1').val(0)
            $('option[value]').hide();
            $('option[value*="-' + val + '"]').show();
        });
    });​
    </script>
    
    0 讨论(0)
  • 2020-12-12 04:12

    You have two elements with an id of "Radio1." You should probably change that.

    Code is here: http://jsfiddle.net/kkk7J/5/

    //record of temporarily removed  options
    var oldoptions = [];
    
    $("[type=radio]").on('click', function () {
       //add all filtered options back
       $("#Select1").append(oldoptions);
    
       //Remove any option whose text does not contain the text of the selected
       //radio button
       oldoptions = $("#Select1 option:not(:contains(" + $(this).val() + "))").detach();
    });​
    

    Detaching the elements is necessary because not all browsers support hiding <option>. Additionally, at least some browsers will keep the currently selected option even if it becomes hidden. These are both solved easily by detaching.

    0 讨论(0)
  • 2020-12-12 04:13

    It could look like this:

    $(function(){
    
        var select = $('#Select1'),
            options = select.find('option');
    
        $('[type="radio"]').click(function(){
            var visibleItems = options.filter('[value*="' + $(this).val()  + '"]').show();
            options.not(visibleItems).hide();
    
            if(visibleItems.length > 0)
            {
                select.val(visibleItems.eq(0).val());
            }
        });
    });​
    

    Demo: http://jsfiddle.net/kkk7J/2/

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