简体   繁体   中英

Rails/jQuery prevent form submission

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()

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM