I'm new to Javascript and HTML.
我对Javascript和HTML不熟悉。
I have the following form in 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>
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':
这是一个名为script.js的文件中的javascript。js文件在header中链接为'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.
当我填写表单并点击submit时,javascript正确地执行了这个函数,但是结果(即向html添加标题)在我点击submit之前会消失几毫秒,然后重新设置为原始页面。
How do I make it so that the insertion of HEADING remains permanent?
如何使标题的插入保持永久性?
9
Move the listener to the form's submit handler. Have the function return false to stop submission:
将侦听器移动到表单的submit处理程序。让函数返回false停止提交:
<form id="date_form" onsubmit="return myFunction();" ...>
Take the listener off the button:
把听众从按钮上拿下来:
<input type="submit">
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.
不要给它命名为submit,因为这会掩盖表单的submit方法,所以您不能调用它。提交按钮只需要一个名称,如果表单上有两个或多个名称,并且您想知道用于提交表单的是哪个名称。
There is an error in your code, it's missing a closing double qoute:
您的代码有一个错误,它缺少一个关闭双qoute:
function myFunction(){
var input = document.getElementById("date").value;
if(input==="February 7"){
document.getElementById('w1').innerHTML = "<h2> HEADING </h2>";
}
return false; // to stop submission
};
This may or may not fix you issues, you haven't said what you are actually trying to do.
这可能解决你的问题,也可能解决不了你的问题,你还没有说过你到底想做什么。
本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2014/02/07/d36b2bbb4dddff6e439562e60f0a67b8.html。