简体   繁体   English

如何使用JavaScript禁用HTML页面刷新

[英]How to disable a html page refresh with javascript

I have this function, but I want to disable the refresh of the page.. I have I button and when I click it it shows the second form and hide the first one... But it shows the second form just for a second, and then display the first form again. 我有此功能,但我想禁用页面刷新。.我有I按钮,当我单击它时,它显示第二个表单并隐藏第一个表单...但是它仅显示第二个表单,然后再次显示第一个表单。 I think it is reloading the page...But is there any way to stop it.. this is my function 我认为它正在重新加载页面...但是有什么办法可以停止它..这是我的功能

 function displayform2() {
     /*For desktop*/
     var inputs = [
         document.getElementById('firstname1'),
         document.getElementById('lastname1'),
         document.getElementById('cel1'),
         document.getElementById('email1'),
     ];
     var nr = 0; /*to keep the number of fields empty*/
     for (var i = 0; i < inputs.length; i++)
         if (inputs[i].value == '') {
             nr++;
         }
     if (document.getElementById('desktop1').style.display == 'block') {
         if (nr > 0) {
             document.getElementById('totalfield1').style.display = 'block';
         } else {
             document.getElementById('desktop1').style.display = 'none';
             document.getElementById('desktop2').style.display = 'block';
             document.getElementById('desktop3').style.display = 'none';
         }
     }

HTML HTML

  <input type="button" value="Proceed"  id="submit1" onclick="displayform2()" class="button" style=" margin-top: -40px;margin-left: 60%;width: 25%" disabled>

I am using these button 我正在使用这些按钮

Please help me 请帮我

Show us the HTML. 向我们展示HTML。 Are you using an anchor to call the displayform2() function? 您是否正在使用锚调用displayform2()函数?

If so, make sure to use return false; 如果是这样,请确保使用return false; to prevent the default behaviour (which is to navigate) 防止默认行为(用于导航)

<a href="javascript:displayform2(); return false;">Display form</a>

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

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