简体   繁体   中英

Display div if option is selected in jQuery

I am trying to display a div if one of 2 options is selected on my page. Here is the select menu:

<select id="graph_select">
<option id="pilot_form">Pilot Hours</option>
<option id="client_form">Client Hours</option>
</select>

Here is my first div for the first option:

<div id="pilot_graph_form" align="center" style="margin:0 auto; display:none;">
        <form action="?page=reporter" method="POST" name="graph_form">
            <p>From:</p>
            <select name="start_date">
                <cfloop query="date_worked_menu">
                    <option>#date_worked_menu.date_worked#</option>
                </cfloop>
            </select>
            <br />
            <br />
            <p>To:</p>
            <select name="end_date">
                <cfloop query="date_worked_menu">
                    <option>#date_worked_menu.date_worked#</option>
                </cfloop>   
            </select>
            <br />
            <input class="btn btn-success" type="Submit" name="submit_to_graph" value="Submit" id="submit_to_graph">
        </form>
    </div>

Here is the div for my second option:

<div id="client_graph_form" align="center" style="margin:0 auto; display:none;">
        <form action="?page=reporter" method="POST" name="graph_form_clients">
            <p>From:</p>
            <select name="client">
                <cfloop query="client_menu">
                    <option value="#client_menu.id#">#client_menu.name#</option>
                </cfloop>
            </select>
            <input class="btn btn-success" type="Submit" name="submit_to_graph" value="Submit" id="submit_to_graph">
        </form>
    </div>

And here is my jQuery function I have so far:

<script type="text/javascript">
$(function() {
    $("#graph_select").change(function() {
        if($("#pilot_form").is(":selected")) {
            $("#pilot_graph_form").css({"display":"block"});
        }
        else {
            $("#pilot_graph_form").css({"display":"none"});
        }
        if($("#client_form").is(":selected")) {
            $("#client_graph_form").css({"display":"block"});
        }
        else {
            $("#client_graph_form").css({"display":"none"});
        }
    });
});
</script>

First of all, you should change "id" on your "option" to "value".

Then you can use this:

$(function () {
  $("#graph_select").change(function() {
    var val = $(this).val();
    if(val === "pilot_form") {
        $("#pilot_graph_form").show();
        $("#client_graph_form").hide();
    }
    else if(val === "client_form") {
        $("#client_graph_form").show();
        $("#pilot_graph_form").hide();
    }
  });
});
$(function() {
  $("#graph_select").change(function() {
    if ($("#pilot_form").is(":selected")) {
      $("#pilot_graph_form").show();
      $("#client_graph_form").hide();
    } else {
      $("#pilot_graph_form").hide();
      $("#client_graph_form").show();
    }
  }).trigger('change');
});

DEMO

when changing select box you can fadeIn elements that you want :

$('#graph_select').change(function(){
   var divID = $(this).children('option:selected').attr('id');
   if(divID == 'pilot_form'){
       $('#client_graph_form').fadeOut(1000,function(){
           $('#pilot_graph_form').fadeIn(500);
       });
   }else{
       $('#pilot_graph_form').fadeOut(1000,function(){
           $('#client_graph_form').fadeIn(500);
        });
   }
});

Updated
in other way : it will be better if you use same name with div's id name in options :

<select id="graph_select">
    <option class="pilot_graph_form">Pilot Hours</option>
    <option class="client_graph_form">Client Hours</option>
</select> 

add same class to each <div>

<div id="client_graph_form" class="forms"
...
<div id="pilot_graph_form" class="forms"

jQuery :

$('#graph_select').change(function(){
   var divID = $(this).children('option:selected').attr('class');
   $('.forms').fadeOut(1000,function(){
        $('#'+divID).fadeIn(500);
   });
});

Code is alright, there might be a problem with loading jquery, I'd suggest you to use

    $(window).load(function(){
        //Code
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM