Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
160
rated 0 times [  166] [ 6]  / answers: 1 / hits: 6515  / 10 Years ago, fri, april 25, 2014, 12:00:00

jQUery prevent form submission.



 $('#edit_user_form').submit(function(){
$(textarea[name = user_form_snippet]).enabled = enabled;
return false;
e.preventDefault();
});


Below is my rails form.



<%form_for @user, :html {:id => edit_user_form },
:url => {action => update, :id => @user.id} do |f| %>

<% end %>


My jQuery function is getting called, but for strange reason the form is getting submitted even if use either return false or e.preventDefault()


More From » jquery

 Answers
2

If you want to use e.preventDefault(), you must have it before your return statement, as the return is always the last thing that happens in a function.



You should also update your code to include the e in the anonymous function, otherwise e.preventDefault() doesn't know what e is :)



http://jsfiddle.net/UEK7d/ <- Example fiddle


[#45761] Thursday, April 24, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kinsley

Total Points: 352
Total Questions: 84
Total Answers: 94

Location: Denmark
Member since Tue, Jul 19, 2022
2 Years ago
;