Javascript 如果在 jQuery 中选择了选项,则显示 div

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14325548/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 16:23:30  来源:igfitidea点击:

Display div if option is selected in jQuery

javascriptjquerycsshtml

提问by Christoph Riesterer

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

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

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

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

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

这是我到目前为止的 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>

回答by Bror

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

首先,您应该将“选项”上的“id”更改为“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();
    }
  });
});

回答by kei

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

演示

回答by M Rostami

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 :


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

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

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

jQuery :

jQuery :

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

回答by Shobhit Sharma

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

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

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