繁体   English   中英

如何根据下拉框选择显示/隐藏div

[英]How to show/hide divs based on a dropdown box selection

我知道我不是唯一遇到此问题的人,但是我找不到JavaScript解决方案。

这是我的下拉选择。 现在,我想为每个选项显示一条特定的消息。

<select name="lehrberuf" id="lehrberuf" class="browser-default" required>
    <option value="" disabled selected class="grey-text">Lehrberuf</option>
    <option value="Anlagenfuehrer/-in EFZ">Anlagenführer/-in EFZ</option>
    <option value="Anlagen- und Apparatebauer/-in EFZ">Anlagen- und Apparatebauer/-in EFZ</option>
    <option value="Automatiker/-in EFZ">Automatiker/-in EFZ</option>
    <option value="Elektroinstallateur/-in EFZ">Elektroinstallateur/-in EFZ</option>
    <option value="Elektroplaner/-in EFZ">Elektroplaner/-in EFZ</option>
    <option value="Fachmann/-frau Betriebsunterhalt EFZ">Fachmann/-frau Betriebsunterhalt EFZ</option>
    <option value="Informatiker/-in EFZ">Informatiker/-in EFZ</option>
    <option value="Kauffrau / Kaufmann EFZ">Kauffrau / Kaufmann EFZ</option>
    <option value="Konstrukteur/-in EFZ">Konstrukteur/-in EFZ</option>
    <option value="Kunststofftechnologe/-technologin EFZ">Kunststofftechnologe/-technologin EFZ</option>
    <option value="Laborant/-in EFZ Fachrichtung Chemie">Laborant/-in EFZ Fachrichtung Chemie</option>
    <option value="Logistiker/-in EFZ Fachrichtung Lager">Logistiker/-in EFZ Fachrichtung Lager</option>
    <option value="Mediamatiker/-in EFZ">Mediamatiker/-in EFZ</option>
    <option value="Polymechaniker/-in EFZ">Polymechaniker/-in EFZ</option>
</select>

这应该是每个选项的消息。

<div class="row col s12">
    <div class="hiddenmessage1">You have to select <strong>any one option</strong> so i am here</div>
    <div class="hiddenmessage2">You have selected <strong>red option</strong> so i am here</div>
    <div class="hiddenmessage3">You have selected <strong>green option</strong> so i am here</div>
    <div class="hiddenmessage4">You have selected <strong>blue option</strong> so i am here</div>
    <div class="hiddenmessage5">You have selected <strong>blue option</strong> so i am here</div>
    <div class="hiddenmessage6">You have selected <strong>blue option</strong> so i am here</div>
    <div class="hiddenmessage7">You have selected <strong>blue option</strong> so i am here</div>
    <div class="hiddenmessage8">You have selected <strong>blue option</strong> so i am here</div>
    <div class="hiddenmessage9">You have selected <strong>blue option</strong> so i am here</div>
    <div class="hiddenmessage10">You have selected <strong>blue option</strong> so i am here</div>
    <div class="hiddenmessage11">You have selected <strong>blue option</strong> so i am here</div>
    <div class="hiddenmessage12">You have selected <strong>blue option</strong> so i am here</div>
    <div class="hiddenmessage13">You have selected <strong>blue option</strong> so i am here</div>
    <div class="hiddenmessage14">You have selected <strong>blue option</strong> so i am here</div>
</div>

我需要有关JS的帮助。

先感谢您。

尝试:

 $('#lehrberuf ').on('change',function(){
   var index = $('#lehrberuf option').index($(this).find(':selected'));
 console.log(index);
   $('.hiddenmessage'+(index+1)).toggle();
});

您可以使用

$(document).ready(function(){
    $('#lehrberuf').on('change',function(){
        var getIndex = $(this).find('>option:selected').index();
        $('.row > div').hide(); // you can use slideDown() or fadeOut() instead of .hide()
        $('.row > div').eq(getIndex).show(); // you can use slideUp() or fadeIn() instead of .hide()
    });
});

工作演示

如果可以避免的话,有这么多的div是一种糟糕的编码实践。 假设您需要更改布局或其他更改,您必须执行10次以上或更改10次以上的类定义。

您的下拉菜单只有一个选项,那么为什么不只有一个div并替换内容呢?

您有html之类的代码:

<select onchange="showText(this)">
   <option value="my text">text</option>
<select>

<div id="dvContent">select an option!</div>

然后javascript:

<script>
  function showText(dd) {
     var sel = dd.options[dd.selectedIndex].value,
     dv = document.getElementById("dvContent");
     dv.innerHTML = sel.value;
  }
 </script>

请下次再试一次,其他人不会为您编码。 学习的唯一方法就是犯错误。

这不是使用jQuery ,我个人认为最好是正确学习然后再使用jQuery以方便使用。 但是如果您愿意,可以使用jQuery将其最小化。

尝试这个:

$('#lehrberuf').change(function() {

    var selected_index = $('#lehrberuf option:selected').index();
var text_in_div = $('#messages > div:eq('+selected_index+')').text();
alert(text_in_div);

});​

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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