Display div if a specific select option value is selected

前端 未结 7 1461
执笔经年
执笔经年 2021-02-02 03:57

I am trying to display a div if user select a specific option value from a select drop down list.

Example:

The select drop down list consist of dynamic names fet

相关标签:
7条回答
  • 2021-02-02 04:00
    <select id="getFname">
    <option value="1">Jay</option>
    <option value="4">Sam</option>
    <option value="0">Admin</option>
    </select>
    
    
    <div id="admDivCheck" style="display:none;">
    admin selected
    </div>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
    
    <script>
    $("#getFname").on("change",function(){"0"===$(this).val()?$("#admDivCheck").show():$("#admDivCheck").hide()});
    </script>
    

    Demo: https://jsfiddle.net/kingtaherkings/swzmxkej/

    0 讨论(0)
  • 2021-02-02 04:01

    I think you need like this

    Just Change this line:

    JS

    admOptionValue = document.getElementById("getFname").value;
    //alert(admOptionValue);
    
    if(admOptionValue == 0){
         document.getElementById("admDivCheck").style.display = "";
    }
    else{
         document.getElementById("admDivCheck").style.display = "none";
     }
    

    And also in HTML

    onchange="admSelectCheck(this);"

    see Demo

    0 讨论(0)
  • 2021-02-02 04:03

    Using this.select is incorrect.

    Here is the correct code:

    HTML

    <select id="getFname" onchange="admSelectCheck(this);">
    <option value="1">Jay</option>
    <option value="4">Sam</option>
    <option id="admOption" value="0">Admin</option>
    </select>
    
    
    <div id="admDivCheck" style="display:none;">
    admin selected
    </div>
    

    JS

    function admSelectCheck(nameSelect)
    {
        console.log(nameSelect);
        if(nameSelect){
            admOptionValue = document.getElementById("admOption").value;
            if(admOptionValue == nameSelect.value){
                document.getElementById("admDivCheck").style.display = "block";
            }
            else{
                document.getElementById("admDivCheck").style.display = "none";
            }
        }
        else{
            document.getElementById("admDivCheck").style.display = "none";
        }
    }
    

    See the demo on JSFiddle.

    0 讨论(0)
  • Try

    <select id="getFname" onchange="admSelectCheck(this);">
        <option value="1">Jay</option>
        <option value="4">Sam</option>
        <option id="admOption" value="0">Admin</option>
    </select>
    
    
    <div id="admDivCheck" style="display:none;">
        admin selected
    </div>
    

    And

    function admSelectCheck(nameSelect)
    {
        var val = nameSelect.options[nameSelect.selectedIndex].value;
        document.getElementById("admDivCheck").style.display = val == '0' ? "block" : 'none';
    }
    

    Demo: Fiddle

    0 讨论(0)
  • 2021-02-02 04:07

    try this:

    JS:

    function admSelectCheck(nameSelect)
    {
        if(nameSelect){
            admOptionValue = nameSelect.value;
            if(admOptionValue != 0){
                nameSelect.style.display = "";
            }
            else{
                nameSelect.style.display = "none";
            }
        }
        else{
            nameSelect.style.display = "none";
        }
    }
    

    HTML:

    <select id="getFname" onchange="admSelectCheck(this);">
    <option value="1">Jay</option>
    <option value="4">Sam</option>
    <option id="admOption" value="0">Admin</option>
    </select>
    
    <div id="admDivCheck" style="display:none;">
    admin selected
    </div>
    
    0 讨论(0)
  • 2021-02-02 04:07

    You can attach change event handler on body load event and hide/unhide <div> based on selection:

    HTML

    <select id="getFname">
      <option value="1">Jay</option>
      <option value="4">Sam</option>
      <option id="admOption" value="0">Admin</option>
    </select>
    
    
    <div id="admDivCheck" style="display:none;">
       admin selected
    </div>
    

    Put the following code in Head tag:

    <script type="text/javascript">
    
    function onload() {
       document.getElementById("getFname").onchange = function (e) {
       if (this.value == 0) {
         document.getElementById("admDivCheck").style.display="";
       } else {
         document.getElementById("admDivCheck").style.display="none";
      }
     };
    
    }
    
    </script>
    

    Call the onload function in body:

    <body onload="onload()">
    

    jsfiddle demo

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