jQuery show/hide a div based on select value

后端 未结 7 812
走了就别回头了
走了就别回头了 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:37

    That could be done with `toggle()` as a shorthand :

    $('#privileges').change(function() {
      $('.resources').toggle($(this).val() == 'custom');
    });
    

    If you want to toggle the display in the page load you could trigger the same change() event, like :

    $('#privileges').change(function() {
      $('.resources').toggle($(this).val() == 'custom');
    }).change();
    

    $('#privileges').change(function() {
      $('.resources').toggle($(this).val() == 'custom');
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <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>

    0 讨论(0)
  • 2020-12-10 13:40

    You can clean up the HTML by dropping the onClick and removing the IDs from the options.

    HTML:

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

    And your JavaScript could simply do this:

    $('#privileges').on('change', function() {
            if($(this).val() === 'all') {
                $('.resources').hide();
            } else {
                $('.resources').show();
            }
     });
    
    0 讨论(0)
  • 2020-12-10 13:49

    You will want to add the event handler:

    $("#privileges").change(craateUserJsObject.ShowPrivileges);
    

    Then, within your ShowPrivileges funciton:

    var val = $("#privileges").val();
    if(val == "whatever")
        //do something
    else
        //do something
    
    0 讨论(0)
  • 2020-12-10 13:50

    You need to use val method:

    var Privileges = jQuery('#privileges');
    var select = this.value;
    Privileges.change(function () {
        if ($(this).val() == 'custom') {
            $('.resources').show();
        }
        else $('.resources').hide(); // hide div if value is not "custom"
    });
    

    http://jsfiddle.net/RWUdb/

    0 讨论(0)
  • 2020-12-10 13:51
      $("#privileges").change(function(){
         var select=  $(this).val();
           if(select=='custom'){
              //some code
             }
        }); 
    

    or

      var select=$('#privileges :selected').val()
        if(select=='custom'){
        //some code
      }
    
    0 讨论(0)
  • 2020-12-10 13:57
    Privileges.on('change',function(){
        if($(this).val()=='custom'){
            $('.resources').show();
        }else{
            $('.resources').hide();
        }
    })
    
    0 讨论(0)
提交回复
热议问题