Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

preventDefault not work in submit button

I have html form and when I clicked submit button page goes to insert.php I don't wont it preventDefoult don't work I have this script html

HTML

<form action="insert.php" method="post">
<input  type="text" name="username" placeholder=""><br />
<input  type="text" name="name" placeholder=""><br />
<input  type="text" name="lastname" placeholder=""><br />
<input id="mail" name="email" type="text" placeholder="E-mail"><br />
<input id="mail_1" type="text" placeholder="reply E-mail"><br />
<input id="password" name="password" type="password" placeholder=""><br />
<input id="password_1"  type="password" placeholder=""><br /> 
<input id="submit" type="submit" value="registration">
</form>

jquery

$("#submit").click(function(event){
    event.preventDefault();
});
like image 567
Val Do Avatar asked Feb 17 '14 19:02

Val Do


People also ask

How do I stop a form from submitting in jquery?

We use the preventDefault() method with this event to prevent the default action of the form, that is prevent the form from submitting.

Does preventDefault stop form submission?

Definition and Usage. The preventDefault() method cancels the event if it is cancelable, meaning that the default action that belongs to the event will not occur. For example, this can be useful when: Clicking on a "Submit" button, prevent it from submitting a form.

What does preventDefault () do?

preventDefault() The preventDefault() method of the Event interface tells the user agent that if the event does not get explicitly handled, its default action should not be taken as it normally would be.


Video Answer


2 Answers

Instead of listening for the button click you need to listen for <form> submit:

$("form").submit(function(event){
    event.preventDefault();
});

Modern jQuery (1.7 or later):

$("form").on('submit', function(event){
    event.preventDefault();
});
like image 191
MonkeyZeus Avatar answered Sep 17 '22 15:09

MonkeyZeus


I think you just missed the event which is canceling

$( "#target" ).submit(function( event ) {
  event.preventDefault();

});

like image 32
Paulo Lima Avatar answered Sep 18 '22 15:09

Paulo Lima