内容提交并运行功能后立即消失
我是JavaScript和HTML的新手。
我在html中有以下表格:
<div id="form-select">
<form id="date_form" action="" METHOD="POST">
<datalist id="dates">
<option value="February 7">February 7</option>
<option value="February 14">February 14</option>
<option value="February 21">February 21</option>
<option value="February 28">February 28</option>
</datalist>
<input type="text" class="input" name="data" id="date" value="" list="dates" placeholder="pick a date"><br>
<input type="submit" name="submit" onClick="myFunction()"/>
</form>
</div>
这是一个名为script.js的文件中的javascript。 js文件在标题中链接为'script type =“ text/javascript” src =“ script.js':
function myFunction(){
var input = document.getElementById("date").value;
if(input==="February 7"){
document.getElementById('w1').innerHTML = "<h2> HEADING </h2>;
}
};
当我填写表单并点击提交时,javaScript正确执行该功能,但是结果(即添加标题 发生了几毫秒。
在html上,在我击中提交之前消失并重置原始页面之前,
I'm new to Javascript and HTML.
I have the following form in HTML:
<div id="form-select">
<form id="date_form" action="" METHOD="POST">
<datalist id="dates">
<option value="February 7">February 7</option>
<option value="February 14">February 14</option>
<option value="February 21">February 21</option>
<option value="February 28">February 28</option>
</datalist>
<input type="text" class="input" name="data" id="date" value="" list="dates" placeholder="pick a date"><br>
<input type="submit" name="submit" onClick="myFunction()"/>
</form>
</div>
Here's the javascript in a file called script.js. The js file is linked in the header as 'script type="text/javascript" src="script.js':
function myFunction(){
var input = document.getElementById("date").value;
if(input==="February 7"){
document.getElementById('w1').innerHTML = "<h2> HEADING </h2>;
}
};
When I fill out the form and hit submit, the javascript correctly executes the function, but the result (i.e. adding HEADING to the html) happens for a couple of milliseconds before disappearing and resetting to the original page before I had hit submit.
How do I make it so that the insertion of HEADING remains permanent?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将听众移至表格的提交处理程序。让函数返回false停止提交:
取下侦听器按钮:
不要给它一个名称提交,因为这将掩盖表单的提交方法,以便您可以' t打电话。提交按钮仅在表格上有两个或多个时才需要一个名称,并且您想知道哪个用于提交表格。
代码中存在错误,它缺少闭合双Qoute:
这可能会解决或可能不会解决问题,您没有说出您实际上要做的事情。
Move the listener to the form's submit handler. Have the function return false to stop submission:
Take the listener off the button:
Do not give it a name of submit as that will mask the form's submit method so you can't call it. Submit buttons only need a name if there are two or more on a form and you want to know which one was used to submit the form.
There is an error in your code, it's missing a closing double qoute:
This may or may not fix you issues, you haven't said what you are actually trying to do.
在功能中添加此行:
Add this line inside the function: