简体   繁体   English

协助js函数/警报擦除表单输入

[英]assist with js function/alert erasing form input

I came across something weird that is making me realize I do not know JS stuff as well as i should. 我遇到了一些奇怪的事情,这使我意识到我不太了解JS。 And so I thought I would ask a question about what is going on to see if that will help. 因此,我想问一个问题,看看是否会有所帮助。 I have a simple form with three "textarea" inputs and i have a "save" button. 我有一个带有三个“ textarea”输入的简单表单,并且有一个“保存”按钮。 the save button triggers a js function that gathers the what is on the inputs and the does an alert showing them... 保存按钮触发一个js函数,该函数收集输入中的内容并执行警报以显示它们...

very simple application but what I was not expecting was that when you "close" the alert box; 非常简单的应用程序,但是我没想到的是,当您“关闭”警报框时; all the data entered on the form is erased... 表格上输入的所有数据都会被删除...

here is my code: 这是我的代码:

<form>

<strong>Make</strong><br/>
<textarea id="make_notes" rows="4" cols="50">

</textarea><br/><br/>

<strong>Model</strong><br/>
<textarea id="model_notes" rows="4" cols="50">

</textarea><br/><br/>

<strong>Features</strong><br/>

<textarea id="features_notes" rows="4" cols="50">

</textarea>

<br/><br/>

<button onclick="side_form_js2()">Save2</button>

</form>

<script>
function side_form_js2() {

var element1 = document.getElementById("make_notes");
    var make_notes_var = element1.value;

var element2 = document.getElementById("model_notes");
    var model_notes_var = element2.value;

    var element3 = document.getElementById("features_notes");
    var features_notes_var = element3.value;

alert(make_notes_var + "I am an alert box!22222" + model_notes_var +   features_notes_var);
}
</script>

So I am really uncertain what is making the form input be erased. 所以我真的不确定是什么使表格输入被擦除。 It is almost like the page is being reloaded but I dont think that is the case. 几乎就像页面正在重新加载,但我认为情况并非如此。 Can anyone shed any light on this? 谁能对此有所启示? Thanks... 谢谢...

PS dont know if this makes any difference but just running this locally using Xampp on my PC... PS不知道这是否有什么区别,但只是在我的PC上使用Xampp在本地运行了...

the default is reload, change your code to this and it should work 默认是重载,将您的代码更改为此,它应该工作

<button onclick="return side_form_js2()">Save2</button>

and then in your function add return false after the alert like this: 然后在您的函数中在警报之后添加return false,如下所示:

alert(make_notes_var + "I am an alert box!22222" + model_notes_var +   features_notes_var);
return false;

A button element with no type attribute specified represents the same thing as a button element with its type attribute set to "submit". 未指定类型属性的按钮元素表示与类型属性设置为“提交”的按钮元素相同的事物。 If you want to use a button in a form without it submitting use type="button". 如果要在表单中使用按钮而不提交,请使用type =“ button”。

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

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