admin管理员组

文章数量:1317910

I would like to add some inline javascript that alerts the user with a message AND then returns false, preventing the form from submitting.

onsubmit="return alert('You must be logged in to register'); return false"

With the code above, I get the alert but the form still submits. I would like to retain the inline JS if possible. I searched around but couldn't find an inline example that follows this pattern. I know how to achieve this by executing a function with onsubmit, but again, would like to retain inline if possible.

I would like to add some inline javascript that alerts the user with a message AND then returns false, preventing the form from submitting.

onsubmit="return alert('You must be logged in to register'); return false"

With the code above, I get the alert but the form still submits. I would like to retain the inline JS if possible. I searched around but couldn't find an inline example that follows this pattern. I know how to achieve this by executing a function with onsubmit, but again, would like to retain inline if possible.

Share Improve this question asked Jul 11, 2014 at 17:45 cpcdevcpcdev 1,2223 gold badges20 silver badges52 bronze badges 1
  • 1 Why are you using return alert() and not alert()? Right now, you are returning two values, meaning that the second one won't ever get returned. – Max Commented Jul 11, 2014 at 17:47
Add a ment  | 

3 Answers 3

Reset to default 2

Try this :

<form onsubmit=" alert('You must be logged in to register');return false;"><input type="submit" /></form>

"Return" stops the stack instruction.

You can not return twice from one function. Get rid of first return and it'll work as charm

by jquery you can do this:

$("#inputID").submit(function(e){
  e.preventDefault();

  ...do whatever you want...

});

本文标签: javascriptonSubmit Alert and Return False using inline JSStack Overflow