简体   繁体   English

我想在javascript中隐藏按钮

[英]I want to hide the button in javascript

I have a HTML form code is as follows 我有一个HTML表单代码如下

   <div id="foo" style="display:none;">
   <input type="text" name="name">
   <input type="text" name="name1">
   <input type="text" name="name2">
   </div>
   <input type="button" onclick="toggle_visibility('foo');" 
   value="Add more" />   

Upon clicking add more all the above textboxes, but the button is still visible and i want to hide that button 单击添加更多上述所有文本框后,该按钮仍然可见,我想隐藏该按钮

Following is the javascript 以下是JavaScript

function toggle_visibility(id) {
       var e = document.getElementById(id);
       if(e.style.display == 'block')
          e.style.display = 'none';
       else
          e.style.display = 'block';
    }

Try this : Use this object to get button and change its display property. 尝试以下操作:使用this对象获取按钮并更改其显示属性。

function toggle_visibility(id) {
       //hide button
       this.style.display = 'none';
       var e = document.getElementById(id);
       if(e.style.display == 'block')
          e.style.display = 'none';
       else
          e.style.display = 'block';
    }

OR - keep button inside foo div and your code will work as it is 或者-将按钮保留在foo div ,您的代码将按原样工作

<div id="foo" style="display:none;">
   <input type="text" name="name">
   <input type="text" name="name1">
   <input type="text" name="name2">

   <input type="button" onclick="toggle_visibility('foo');" 
   value="Add more" /> 
</div>

You tagged the question with jQuery , so why not use it? 您用jQuery标记了问题,为什么不使用它呢?

 $("input[type=button]").click(function() { $("#foo").show(); $(this).hide(); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="foo" style="display:none;"> <input type="text" name="name"> <input type="text" name="name1"> <input type="text" name="name2"> </div> <input type="button" value="Add more" /> 

 function toggle_visibility(btn,id) { //hide button btn.style.display = 'none'; var e = document.getElementById(id); if(e.style.display == 'block') e.style.display = 'none'; else e.style.display = 'block'; } 
 <div id="foo" style="display:none;"> <input type="text" name="name"> <input type="text" name="name1"> <input type="text" name="name2"> </div> <input type="button" onclick="toggle_visibility(this,'foo');" value="Add more" /> 

Try this. 尝试这个。

Give id to your tag 为您的标签提供ID

<button type="button" id='btnclick'>Add More</button>

use below jquery 使用下面的jQuery

$("#btnclick").click(function(){

    $("#btnclick").hide();
}
});

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

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