[form html]

Why the form submission event does not fire (jQuery)?

I have a form + layout:

<form ...> <div id="editor"> [form html] <input type="submit" value="Submit form" /> </div> </form> 

And the following javascript:

 $(function() { var form = $('#editor').parents('form'); alert(form.length); // this alerts "1" $(document).on('submit', 'form', function() { alert('document form submit fired'); // this works as expected (alerts) }); form.on('submit', function() { alert('selected form submit fired'); // this is never alerted }); }); 

This form does not load via ajax. When the page loads, the first dialog warns "1". However, when you submit the form, only one warning is triggered - one that triggers the submission for all forms in the document.

Why is this going to happen?

+6
source share
2 answers

He is working . Something else is happening that is stopping the second warning from firing.

+7
source

Invalid shape selection.

Try to do it

 $("form").on('submit', function() { 

Pretty sure it should work

In fact, if nothing loads via ajax or dynamically via javascript

You can just do

 $("form").submit(function() { 

EDIT

Scratch mine above. I did not see you set the value of the form. Check out http://jsfiddle.net/s3fvM/1/ . Everything seems to work just fine. both shoot and warn.

0
source

Source: https://habr.com/ru/post/908258/


All Articles