onsubmit刷新html表单

发布于 2025-01-03 12:49:25 字数 378 浏览 0 评论 0原文

我正在尝试使用 JavaScript 来提交表单的数据。这是 HTML。

<form onsubmit="post();">
//input fields here
</form>

这是 post() 函数的 Javascript。

var post = function() {
alert('the form was submitted');
return false;
}

我的问题是 Javascript 运行,但表单仍然处理并刷新页面。

我放置了 return false; 代码,希望它能阻止表单刷新。

I'm trying to use Javascript to submit the form's data. Here's the html.

<form onsubmit="post();">
//input fields here
</form>

Here's the Javascript for the post() function.

var post = function() {
alert('the form was submitted');
return false;
}

My issue is that the Javascript runs but the form still processes and refreshes the page..

I put the return false; code in hoping it would stop the form from refreshing.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

2025-01-10 12:49:25

您必须将 return false 部分放在 onsubmit 处理程序中的 post() 函数之后,如下所示:

<form onsubmit="post();return false;">
//input fields here
</form>

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>
淡紫姑娘! 2025-01-10 12:49:25

让你的 js 远离 DOM。

<form id="myform" action="somepage.php" method="post">
//input fields
</form>

jQuery:

$('#myform').submit(function(event){
    alert('submitted');
    event.preventDefault();
});

Keep your js out of the DOM.

<form id="myform" action="somepage.php" method="post">
//input fields
</form>

JQuery:

$('#myform').submit(function(event){
    alert('submitted');
    event.preventDefault();
});
煞人兵器 2025-01-10 12:49:25

您实际上需要从内联 dom-0 处理程序返回 false。所以更改

onsubmit = "post();">

onsubmit = "return post();">

或者你可以给你的表单一个 id 并执行以下操作:

<form id="form1" onsubmit = "post();">

然后从你的 dom 准备好的安全位置:

document.getElementById("form1").onsubmit = post;

You need to actually return false from your inline dom-0 handler. So change

onsubmit = "post();">

to

onsubmit = "return post();">

Or you could give your form an id and do this:

<form id="form1" onsubmit = "post();">

Then from a safe location in which your dom is ready:

document.getElementById("form1").onsubmit = post;
西瑶 2025-01-10 12:49:25

由于您添加了 jQuery 标记,因此这是执行此操作的最佳方法:
不引人注目的事件

$('form').submit(function(){
        alert('the form was submitted');
        return false;
    });

应该以您的方式进行;

<form onsubmit="return post();">

Since you added the jQuery tag, this it the best way to do this:
unobtrusive event attach

$('form').submit(function(){
        alert('the form was submitted');
        return false;
    });

In your's way it should be;

<form onsubmit="return post();">
缱绻入梦 2025-01-10 12:49:25

由于这篇文章带有 jQ​​uery 标签,我将提供以下解决方案:

$('form').submit(function(e){
  //prevent the form from actually submitting.
  e.preventDefault();
  //specify the url you want to post to.
  //optionally, you could grab the url using $(this).attr('href');
  var url = "http://mysite.com/sendPostVarsHere";
  //construct an object to send to the server
  //optionally, you could grab the input values of the form using $(this).serializeArray()
  var postvars = {};
  //call jquery post with callback function
  $.post(url, postvars, function(response){
    //do something with the response
    console.log(response);
  }, 'json')
});

Since this post is tagged with jQuery, I'll offer the following solution:

$('form').submit(function(e){
  //prevent the form from actually submitting.
  e.preventDefault();
  //specify the url you want to post to.
  //optionally, you could grab the url using $(this).attr('href');
  var url = "http://mysite.com/sendPostVarsHere";
  //construct an object to send to the server
  //optionally, you could grab the input values of the form using $(this).serializeArray()
  var postvars = {};
  //call jquery post with callback function
  $.post(url, postvars, function(response){
    //do something with the response
    console.log(response);
  }, 'json')
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文