简体   繁体   English

使用javascript根据html中的下拉选择显示和隐藏div元素

[英]Show and hide div elements based on dropdown selection in html using javascript

I have a question using javascript to show hide a div when using a dropdown. 我在使用下拉菜单时使用javascript显示隐藏div有一个问题。 Code works for links and buttons but im asking if there's any way to rewrite it so it can use the SELECT option. 代码适用于链接和按钮,但即时消息询问是否有任何方法可以重写它,以便可以使用SELECT选项。 Like if i select 'Show' from dropdown it will show me the div containing 'Hello world!' 就像我从下拉列表中选择“显示”一样,它将向我显示包含“ Hello world!”的div。

My current Javascript: 我目前的Javascript:

<script>
function showMe(id) {
    var e = document.getElementById(id);
    if(e.style.display == "block") {
        e.style.display = "none";
    } else {
        e.style.display = "block";
    }
}
</script>

And the index.html contains: 并且index.html包含:

<select>
    <option>Hide</option>
    <option onselect="showMe('idShowMe')">Show</option>
</select>

<div id="idShowMe" style="display: none">
    <b>Hello world!</b>
</div>

You can change your code 您可以更改代码

 function showMe(e) { var strdisplay = e.options[e.selectedIndex].value; var e = document.getElementById("idShowMe"); if(strdisplay == "Hide") { e.style.display = "none"; } else { e.style.display = "block"; } } 
 <select onchange="showMe(this);"> <option>Hide</option> <option>Show</option> </select> <div id="idShowMe" style="display: none"> <b>Hello world!</b> </div> 

    <select onchange="showMe(this)">
    <option>Hide</option>
    <option >Show</option>
</select>

<div id="idShowMe" style="display: none">
    <b>Hello world!</b>
</div>



function showMe(selectedOption) {

    if(selectedOption.value=="Hide") {
        document.getElementById('idShowMe').style.display = 'none';
    } else {
        document.getElementById('idShowMe').style.display = 'block';
    }
}

This can be done with onchange event and then checking the selected option. 这可以通过onchange事件然后检查选定的选项来完成。 http://jsfiddle.net/hanno_drefke/dwfr224q/ http://jsfiddle.net/hanno_drefke/dwfr224q/

the Javascript code: JavaScript代码:

var current;
function showMe(element) {
  if(current!==undefined){
    document.getElementById(current).setAttribute('style','display:none');
  }
  var fetchMe = element.options[element.selectedIndex].getAttribute('data-show');
  if(fetchMe!==null){
    document.getElementById(fetchMe).setAttribute('style','display:block'); 
    current=fetchMe;
  }
}

and minimal change ot your HTML Code: 和最小的更改您的HTML代码:

<select onchange="showMe(this)">
    <option>Hide</option>
    <option data-show="idShowMe">Show</option>
</select>

<div id="idShowMe" style="display: none">
    <b>Hello world!</b>
</div>

With this solution are free to connect any layer with the corresponding id to the option. 使用此解决方案,可以自由地将具有相应ID的任何层连接到该选项。 Just insert the data-show value and create a div with the id: http://jsfiddle.net/hanno_drefke/dwfr224q/1/ 只需插入数据显示值并创建一个具有以下ID的div: http : //jsfiddle.net/hanno_drefke/dwfr224q/1/

JS Fiddle for the same JS Fiddle同样

<select id="mySelect" onchange="showMe('idShowMe')">
    <option>Hide</option>
    <option>Show</option>
</select>

<div id="idShowMe" style="display: none">
    <b>Hello world!</b>
</div>

<script>
function showMe(id) {
    var e = document.getElementById(id);
    if(e.style.display == "block") {
        e.style.display = "none";
    } else {
        e.style.display = "block";
    }
}
</script>

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

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