How to show/hide DIV on selection of ANY drop-down value?

前端 未结 9 930
陌清茗
陌清茗 2020-12-10 22:18

I have found many answers to the question:

How do I show/hide DIV on selection of \"Other\" dropdown value.

However I cannot fin

相关标签:
9条回答
  • 2020-12-10 23:02

    Javascript

    var elem = document.getElementById("security_question_1");
    elem.onchange = function(){
        var hiddenDiv = document.getElementById("showMe");
        hiddenDiv.style.display = (this.value == "") ? "none":"block";
    };
    

    HTML

    <select class="default" id="security_question_1" name="security_question_1">
            <option value="" selected>Select question...</option>
            <option value="1">Question One</option>
            <option value="2">Question Two</option>
            <option value="3">Question Three</option>
            <option value="4">Question Four</option>
            <option value="5">Question Five</option>
            <option value="6">Question Six</option> 
        </select>
    <div id="showMe">Value Selected</div>
    

    CSS

    #showMe{
        display:none;
    }
    

    FIDDLE http://jsfiddle.net/Sj5FN/1/

    0 讨论(0)
  • 2020-12-10 23:02

    Try this -

    $( "#security_question_1" ).change(function () {
        if($( "option:selected", this ).text()=="text you want to match"){
           $("#div-id").hide();
        }else{
           $("#div-id").show();        
        }
    });
    
    0 讨论(0)
  • 2020-12-10 23:03

    Try this:

    HTML:

    <div>
            <select>
                <option value="choose">Choose Color</option>
                <option value="red">Red</option>
                <option value="green">Green</option>
                <option value="blue">Blue</option>
            </select>
        </div>
        <div class="choose box">You have to select <strong>any one option</strong> so i am here</div>
        <div class="red box">You have selected <strong>red option</strong> so i am here</div>
        <div class="green box">You have selected <strong>green option</strong> so i am here</div>
        <div class="blue box">You have selected <strong>blue option</strong> so i am here</div>
    

    Javascript:

    $(document).ready(function(){
            $("select").change(function(){
                $( "select option:selected").each(function(){
                    if($(this).attr("value")=="red"){
                        $(".box").hide();
                        $(".red").show();
                    }
                    if($(this).attr("value")=="green"){
                        $(".box").hide();
                        $(".green").show();
                    }
                    if($(this).attr("value")=="blue"){
                        $(".box").hide();
                        $(".blue").show();
                    }
                    if($(this).attr("value")=="choose"){
                        $(".box").hide();
                        $(".choose").show();
                    }
                });
            }).change();
        });
    

    Working Demo

    as per your requirement

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