Event.preventDefault () running in Chrome, not Firefox for submit button

I have the following submit button:

<input type="submit" name="submit" value="Send" class="mybutton" /> 

When I use the following code

 $(document).ready(function(){ $("#submit").submit(function(){ event.preventDefault(); console.log('test'); } }); 

My page refreshes in Firefox, but not in Chrome. Can someone point me in the right direction?

I would like to use the submit button to call jQuery.ajax (not regular form submission)

+6
source share
5 answers

You need to make sure that you add event to the parameter list of the event function. I think some browsers have a global event , so it works on some browsers.

 $(document).ready(function(){ $("#submit").submit(function(event){ // The event is passed to this function event.preventDefault(); console.log('test'); } }); 
+18
source

Did you try to pass the event through a variable?

 $(document).ready(function(){ $("#submit").submit(function(e){ e.preventDefault(); console.log('test'); } }); 
+7
source

Pass the event parameter in the function callback. try it

 $(document).ready(function(){ $("#submit").submit(function(event){ event.preventDefault(); console.log('test'); } }); 
+3
source

Just in the onclick function for the submit button, write this:

 onclick="return myfunction();" 

and inside function statements

 myfunction () { return false;}; 
+1
source

You have not added an event to the function parameters.
fixed version:

 $(document).ready(function(){ $("#submit").submit(function(event){ event.preventDefault(); console.log('test'); } }); 
0
source

Source: https://habr.com/ru/post/905841/


All Articles