10

如果在我的页面上选择了 2 个选项之一,我正在尝试显示一个 div。这是选择菜单:

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

这是我第一个选项的第一个 div:

<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>

这是我的第二个选项的 div:

<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>

这是我到目前为止的 jQuery 函数:

<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>
4

4 回答 4

17

首先,您应该将“选项”上的“id”更改为“值”。

然后你可以使用这个:

$(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();
    }
  });
});
于 2013-01-14T20:01:11.500 回答
7
$(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');
});

演示

于 2013-01-14T19:58:36.633 回答
3

更改选择框时,您可以淡入您想要的元素:

$('#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);
        });
   }
});


以其他方式更新: 如果您在选项中使用与 div 的 id 名称相同的名称会更好:

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

为每个添加相同的类<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);
   });
});
于 2013-01-14T19:54:34.710 回答
0

代码没问题,加载jquery可能有问题,建议你使用

    $(window).load(function(){
        //Code
    });
于 2013-01-14T20:02:45.023 回答