6

I`m trying to call a javascript function to validates some data on a form, and if they are ok, fill a form field (hidden field).

After this step I want to clean some form fields and proceed with the post action of the form.

So the result on my controller would be this hidden field, filled by my javascript logic, and the other fields cleared.

How can I accomplish this on Rails 4 ?

Thanks in advance

2 Answers 2

11

you can try something like:

$(function() {

  $('button').click(function(e){
    e.preventDefault();
    //DO SOMETHING
    $('YOURFORM').submit();
  }
})

I hope this helps

Sign up to request clarification or add additional context in comments.

Comments

3

Catching the form submit action can also be handled by attaching an event listener to the form itself. This allows you to use the Rails-included JavaScript driver to do things like disabling the button on submit (e.g. <%= submit_tag "Apply Filters", :data => {:disable_with => 'Applying...'} %>).

$('form#form-id').submit(function() {
    if (valid) {
        return true;
    }
        return false;
    }
})

Note: returning false from the function passed into the event listener does the same thing as invoking preventDefault on the event object.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.