onsubmit刷新html表单
问题描述:
我试图使用Javascript来提交表单的数据。这里是html。
I'm trying to use Javascript to submit the form's data. Here's the html.
<form onsubmit="post();">
//input fields here
</form>
以下是 post()
功能。
var post = function() {
alert('the form was submitted');
return false;
}
我的问题是Javascript运行,但窗体仍然处理并刷新页面..
My issue is that the Javascript runs but the form still processes and refreshes the page..
我把 return false;
代码放在希望它会阻止窗体刷新的地方。
I put the return false;
code in hoping it would stop the form from refreshing.
答
您必须在onsubmit处理程序中的post()函数后面放置返回false部分,如下所示:
You will have to put the return false part after the post() function in the onsubmit handler, like so:
<form onsubmit="post();return false;">
//input fields here
</form>