0

In the following form when I hit submit, it alerts 111, but does not send an ajax request. It instead sends a page-refreshing submit request. Why?

<form id="post_answer_form" method="POST" action="some_url">
    <fieldset>
        <textarea id="qanswer" name="qanswer"></textarea>
        <button type="submit">Post Your Answer </button>
        <button type="submit" style="display: none;">Update </button>
    </fieldset>
</form>

I'm validating it against some rules as follows:

$(document).on('submit', '#post_answer_form', function(){
    alert(111)
    tinymce.triggerSave();
}).validate({
    ignore: '',
    rules: {
        qanswer: {
            required: true,
        }
    },
    submitHandler: function(form) {
        alert(111)
        $.ajax({
            ...
        });
        return false
    },
});
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Praful Bagai
  • 16,684
  • 50
  • 136
  • 267

1 Answers1

0

Your validation is not working because you are not applying validate on form . you can try following code.

$(function(){   
    $('#post_answer_form').submit(function(e) {
        e.preventDefault();    
        tinymce.triggerSave();
        if($('#post_answer_form').valid()){
            // Submit form using ajax or using .submit() 
            $.ajax({
                ...
            });
        }
    }); 

    $('#post_answer_form').validate({
        ignore: '',
        onsubmit : false,
        rules: {
            qanswer: {
                required: true,
            }
        }
    });
});
  • @pythonEnthusiast Have you tried above code ? Code is self explanatory but let me know if you don't understand it . I will put more explanation. – Kandarp Kalavadia Oct 17 '16 at 04:48
  • There's another query of mine related to Jquery Validation. Can you please have a look. http://stackoverflow.com/questions/40092806/jquery-validating-only-one-form-out-of-multiple-forms/40093045#40093045 – Praful Bagai Oct 18 '16 at 09:10