jQuery show/hide a div based on select value

后端 未结 7 850
走了就别回头了
走了就别回头了 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');
    });
    
    
    resources

提交回复
热议问题