jQuery show/hide a div based on select value

后端 未结 7 813
走了就别回头了
走了就别回头了 2020-12-10 13:02

I have a select list with values \'all\' and \'custom\'. On select with value \'custom\' a div with class \'resources\' should appear, and if the value is \'all\' it should

相关标签:
7条回答
  • 2020-12-10 13:59

    You are making this too complicated:

    First off, drop the inline onclick. Since you are using jQuery, attach your handler dynamically.

    <div>
        <label>Privileges:</label>
        <select name="privileges" id="privileges">
            <option id="all" value="all">All</option>
            <option id="custom" value="custom">Custom</option>
        </select>
    </div>
    <div class="resources" style=" display: none;">resources</div>
    

    Secondly, don't listen for the click and then attach the change handler. Attach it directly

    <script>
    jQuery('#privileges').on('change',function(){
        if(jQuery(this).val()=='custom'){
            jQuery('.resources').show();
        } else {
            jQuery('.resources').hide();
        }
    });
    </script>
    
    0 讨论(0)
提交回复
热议问题