如何停止JS和HTML中的表单自动重置? [英] How to stop form auto-reset in JS and HTML?

查看:81
本文介绍了如何停止JS和HTML中的表单自动重置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的表单定义如下:

<form id="form" onsubmit="validate()">

我处理信息,然后按如下所示修改HTML的正文:

I process information and then modify the body of the HTML as follows:

var toRemove = document.getElementById("formContainer");
toRemove.parentNode.removeChild(toRemove);
document.getElementById("main").childNodes[1].innerHTML = text1;
document.getElementById("main").childNodes[3].innerHTML = text2;

这可能会显示一毫秒,但随后页面将重置为空白表格.我可以通过在以后添加警报来解决此问题,但是一旦我关闭警报,页面就会重置.有没有办法阻止它重置?

This displays corectly for a milisecond maybe, but then the page resets to the blank form. I can fix this by adding an alert afterwards, but as soon as I dismiss the alert the page resets. Is there a way to stop it from resetting?

这是完整的代码:

HTML- http://puu.sh/p7HIl/e1abcc7920.html JavaScript- http://puu.sh/p7HJ3/54850f8588.js

HTML - http://puu.sh/p7HIl/e1abcc7920.html JavaScript - http://puu.sh/p7HJ3/54850f8588.js

这也是实时版本-推荐答案

我假设js代码段写在 validate 函数

I presume the js snippets are written inside validate function

如果是这样,则可以使用 event.preventDefault()函数,该函数将阻止默认行为.

If it is so you can use event.preventDefault() function which will block the default behavior .

function validate(event){
  event.preventDefault();
  //Rest of the code
}

注意:这将停止提交表单.但是您可以使用 ajax 发送表单数据

Note: This will stop from submitting the form. But you can use ajax to send form data

这篇关于如何停止JS和HTML中的表单自动重置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆