CSS - Hide elements (class) based on value of select box

前端 未结 3 1567
轮回少年
轮回少年 2021-01-22 11:36

I have a

提交评论

  • 2021-01-22 11:49

    I would use personally create another class for hidden objects (eg 'hiddenClass'), and use jquery similar to the following:

    $( "#typeBox" ).change(function(){
     if($("#typeBox").val()=="other")
     {
         $(".applicableSupportCase").each(function(){
             $(this).addClass("hiddenClass");
         });
     }
    });
    
    0 讨论(0)
  • 2021-01-22 12:02

    Actually, the css attribute selector don't look at value in the way that you are thinking...

    Though Javascript sees the value property as the selected option's value, CSS sees only the markup, and an select[value='something'] attribute selector would actually look for this:

    <select value='something'>
    

    Not for the <option> selected.

    Through css-only you will not be able to change another element using the selected option because the <option> is nested to the <select> tag, and there's no parent navigation selection on css.

    EDIT

    You can, however, mock it up with javascript, and leave your css selector as it is. Just trigger the select's onchange event to set an attribute called value to the DOM select element:

    See Working Fiddle Example

    document.getElementById('typeBox').onchange = function() {
        this.setAttribute('value', this.value);
    };
    
    0 讨论(0)
  • 提交回复
    热议问题