最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Stop redirect in JavaScript - Stack Overflow

programmeradmin0浏览0评论

I have a function which verifies if some fields have been filled out (if length > 0) before submitting. If it fails to submit, I don't want to redirect the client at all. Right now, I have the following:

function onSubmit()
{
  if (verify()) //This function will throw alert statements automatically
  {
     document.getElementById('my_form').submit();
     return void(0);
  }

  else
  {
     document.getElementById('my_form').action = null;
  }

}

However, it doesn't matter if verify() returns true or not, I still redirect the client and wipe her inputted fields. How do I keep the client on the page if a required field is blank? (I don't want to lose her currently filled out form...)

Also, I can't use the slick JQuery libraries, since it's not supported on some older browsers. (I'm trying to capture the most general audience.)

I have a function which verifies if some fields have been filled out (if length > 0) before submitting. If it fails to submit, I don't want to redirect the client at all. Right now, I have the following:

function onSubmit()
{
  if (verify()) //This function will throw alert statements automatically
  {
     document.getElementById('my_form').submit();
     return void(0);
  }

  else
  {
     document.getElementById('my_form').action = null;
  }

}

However, it doesn't matter if verify() returns true or not, I still redirect the client and wipe her inputted fields. How do I keep the client on the page if a required field is blank? (I don't want to lose her currently filled out form...)

Also, I can't use the slick JQuery libraries, since it's not supported on some older browsers. (I'm trying to capture the most general audience.)

Share Improve this question asked Mar 13, 2012 at 17:20 SalSal 3,2697 gold badges33 silver badges41 bronze badges 13
  • 2 Why aren't you returning false? Why return void(0)? Did you try alert(verify()) to see what it returns? Also setting the action to null after the form is submitted is not good. If you have server side code, then it's okay since javascript executes before server side code. If javascript returns false, server side shouldn't execute anyways. – C0D3 Commented Mar 13, 2012 at 17:22
  • 2 What older browsers are not supported by jQuery? It sounds as though you are seriously misinformed. – zzzzBov Commented Mar 13, 2012 at 17:23
  • 1 How is the onSubmit() function being invoked? – Nick Rolando Commented Mar 13, 2012 at 17:24
  • document.getElementById is supported by less browsers than jQuery – qwertymk Commented Mar 13, 2012 at 17:29
  • 1 Just add onSubmit to your <form> tag and route that through your script. This will stop all browsers from submitting: <form onSubmit="runScript();"> – DoctorLouie Commented Mar 13, 2012 at 17:48
 |  Show 8 more ments

3 Answers 3

Reset to default 4

This is how I would try to solve this:

document.getElementById('my_form').onsubmit = function( e ){
    var event = e || window.event;


    // function payload goes here.


    event.returnValue = false;
    if ( event.preventDefault ){ event.preventDefault(); }
    return false;
}

Can be used with event delegation too.

return false to the form!

<form onsubmit="return onSubmit()">

function onSubmit()
{
  if (verify()) //This function will throw alert statements automatically
  {
     return true;
  }

  else
  {
     return false;
  }

}

to stop the form from submitting, return false from your onSubmit

发布评论

评论列表(0)

  1. 暂无评论