I'm running into a bit of trouble while trying to cancel the submit of a form. I've been following this tutorial (even though i'm not making a login script), and it seems to be working for him.
Here's my form:
<form action="index.php" method="post" name="pForm">
<textarea name="comment" onclick="if(this.value == 'skriv här...') this.value='';" onblur="if(this.value.length == 0) this.value='skriv här...';">skriv här...</textarea>
<input class="submit" type="submit" value="Publicera!" name="submit" />
</form>
And here's the jquery:
$(document).ready(function() {
$('form[name=pForm]').submit(function(){
return false;
});
});
I've already imported jQuery in the header and i know it's working. My first thought was that it might be outdated, but it's actually "just" a year ago.
So do anyone see what's wrong?
Thanks in advance.
EDIT: From what i've read the easiest and most appropriate way to abort the submit is to return false? But i can't seem to get it working. I've searched the forum and i've found several helpful threads but none of them actually works. I must be screwing something up.
You have to do a sort of 'double return', or in other words, you have to return what is returned. So, you have your html:
Than you just have a jquery function that is called
onsubmit
as you see above:You can put any sort of conditionals in the function above. It seems you simply want to cancel it though. Hope this helps for others that come across this answer.
How about this one?
Try using event.preventDefault
I also had this problem, my code (that didn't work) was something like this:
and inside the "before_submit" function i had "return false" to stop the form from submitting:
I put "return" when binding the event handler function to the form and it worked:
The function attached to the submit event has to return false (in my case the anonymous function). So...this is one solution to one cause of this problem
I had this same problem and it was down to using Rails and
:remote => true
on the form. Rails jQuery driver was coming in and submitting the form by ajax while my own function was trying to stall the submission process usingreturn false
andevent.preventDefault()
.So look out for frameworks and default actions that interfere with your own javascript.
return false
should work :)Thanks for the respond everybody! A friend of mine tipsed me to add
on the form. That works, but i'd still like to know a not-inline-javascript trick that works.