选择框出现/消失按钮单击

时间:2019-03-14 19:25:30

标签: javascript html

四处搜寻,找不到我想要的东西。我有2个按钮,它们在单击时会拉出两个不同的选择框,而在第二次单击时它们会消失。但是,我希望选择框1在Button单击2时消失,反之亦然:选择框2将在Button单击1时消失。

HTML:

window.onload = function() 
	  { 
      document.getElementById('name1').style.display = 'none';
      document.getElementById('name2').style.display = 'none';
	  };


	  function button1function(id){

      if(document.getElementById(id).style.display == 'none'){
         document.getElementById(id).style.display = 'initial';
      }else{
         document.getElementById(id).style.display = 'none';
      }
      }
	  function button2function(id){
      if(document.getElementById(id).style.display == 'none'){
         document.getElementById(id).style.display = 'initial';
      }else{
         document.getElementById(id).style.display = 'none';
      }
      }
<button type="button" value='hide/show' onclick="button1function('name1')">Button 1</button>
<button type="button" value='hide/show' onclick="button2function('name2')">Button 2</button>

    <select id="name1">
     <option value="">What would you like to know..</option>
    </select>
    <select id="name2">
     <option value="">What would you like to know 2..</option>
    </select>

1 个答案:

答案 0 :(得分:1)

喜欢吗? https://jsfiddle.net/o3btLkpd/

尝试在事件处理程序顶部为按钮1调用此函数:

ws://172.168.41.61

,然后在按钮2的处理程序中显示此信息:

document.getElementById('name2').style.display = 'none';

完整的代码如下:

document.getElementById('name1').style.display = 'none';