简体   繁体   English

仅当从列表中选择特定项目时,如何使div出现

[英]How to make div appear only when specific item from list is selected

I got this code: 我得到以下代码:

<form>
<select name="lelel">
  <option value="x">x</option>
  <option value="xx">xx</option>
  <option value="xxx">xxx</option>
</select>
</form>
<div id="divid">
  LALALALALA
</div>

And my question is, can i make the div "divid" be invisible and then when the user selects the option xxx the div will appear. 我的问题是,我可以使div“ divid”不可见,然后当用户选择xxx选项时,div就会出现。

 $('[name=lelel]').change(function(){ if ( this.value == 'xxx') { $('#divid').show(); }else{ $('#divid').hide(); } }); 
 #divid {display:none;} 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <form> <select name="lelel"> <option value="x">x</option> <option value="xx">xx</option> <option value="xxx">xxx</option> </select> </form> <div id="divid"> LALALALALA </div> 

If you were going to have several divs that appear/disappear based on user selection you could do the follow with the JS code expanding on gibberish's answer 如果您要根据用户选择显示/消失多个div,则可以按照乱码的答案扩展JS代码来执行以下操作

$('select').change(function(){
  var chosen_one = $(this).value();
  $('.toShow').hide();
  $('#'+chosen_one).show();
})

You can then give all the divs that will be shown/hidden a class of toShow and set them as display: none; 然后,您可以为将要显示/隐藏的所有div提供toShow类,并将它们设置为display: none; in the CSS. 在CSS中。 Then as you add more options/divs you don't have to modify your JS or CSS 然后,当您添加更多选项/ div时,无需修改JS或CSS

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

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