本文介绍了如果在jQuery中选择了选项,则显示div的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我的网页上选择了2个选项中的一个,我试图显示div。以下是选择菜单:

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>

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

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>

这是第二个选项的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>

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

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>


推荐答案

option改为value。

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

然后,您可以使用:

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

这篇关于如果在jQuery中选择了选项,则显示div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 05:36