onsubmit refresh html form

﹥>﹥吖頭↗ 提交于 2019-12-01 03:25:05
D. Strout

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>

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();
});

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;

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();">

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')
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!