jquery - How do I stop a remote form from submitting? -


i have form can used both remotely , normally.

= form_for @comment, html: { class: 'comment-form' }, remote: request.xhr? |f|   = f.text_area :body   = f.submit 

i want form submit if body textarea has content:

  $(document).on 'submit', '.comment-form', (e) ->     text = $(this).find('#comment_body').val()     false if text.length < 1 

that code works when form not ajax. when it's remote, fails , form still submits. why? tried:

if text.length < 1   e.preventdefault()   false 

i'm using rails 4 turbolinks.

update: tried binding ajax:beforesend event, still submits:

  $(document).on 'page:load', '.comment-form', ->     $(this).bind 'ajax:beforesend', ->       alert "hi" 

i see no alert...

you potentially implement handler ajax:beforesend event prevent form being submitted. looks submit stops if event handler returns false.

wiki: https://github.com/rails/jquery-ujs/wiki/ajax

example implementation: stop $.ajax on beforesend


Comments

Popular posts from this blog

Unable to remove the www from url on https using .htaccess -