There is a button labeled NEWFORM to create a new form when clicked. Each form has a submit button. When the submit button of each form is pressed, the values โโof this form will be sent via AJAX. My code works well for the first time, but when a new form is created and submitted, all the values โโof all forms will be submitted together.
Here is my snippet:
$(document).ready(function() { $(".newform").click(function() { $(".MyForm") .eq(0) .clone() .show() .insertAfter(".MyForm:last"); }); $(document).on('click', '.MyForm button[type=submit]', function(e) { e.preventDefault() // To make sure the form is not submitted $('.MyForm').each(function() { console.log($(this).serialize()) $.ajax( $(this).attr('action'), { method: $(this).attr('method'), data: $(this).serialize() } ) }); }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.1/jquery.min.js"></script> <span class="newform">NEWFORM+</span> <div class="all"> <form class="MyForm" method="post"> <input type="text" placeholder="name" value="Aynaz" name="a1" /> <select name="Avg"> <option value="1">1</option> <option value="2">2</option> </select> <button type="submit">Submit</button> </form> </div>
You repeat all the ".MyForm" objects in your solution, so they are all presented, you need to first determine the correct form in onClick, and then send:
$(document).ready(function() { $(".newform").click(function() { $(".MyForm") .eq(0) .clone() .show() .insertAfter(".MyForm:last"); }); $(document).on('click', '.MyForm button[type=submit]', function(e) { e.preventDefault() // To make sure the form is not submitted var $frm = $(this).closest('.MyForm'); console.log($frm.serialize()); $.ajax( $frm.attr('action'), { method: $frm.attr('method'), data: $frm.serialize() } ); }); });
$(document).ready(function() { $(".newform").click(function() { $(".MyForm") .eq(0) .clone() .show() .insertAfter(".MyForm:last"); }); $(document).on('click', '.MyForm button[type=submit]', function(e) { e.preventDefault() // To make sure the form is not submitted var $this = $(this).closest("form"); console.log($this.serialize()) $.ajax( $(this).attr('action'), { method: $this.attr('method'), data: $this.serialize() } ) }); });
You could do it sooner
$(document ).on('submit', '.myForm', function(e) { e.preventDefault() $.ajax({ type: 'post', data: $(this).serialize(), url: 'submit.php' }) })
The problem is your contextual use of $(this)
$(this)
Source: https://habr.com/ru/post/1261692/More articles:ORA-32795: cannot be inserted into generated identity column - sqlSwift: make translucent overlapping lines of the same color without changing color when crossing - swiftlet webpack print individual compiled files except the package - javascriptFast drawing of translucent lines, how to make overlapping parts not dark? - iosperl6 How to read from stdin and take command line arguments? - stdinLinux Bash: move several different files to the same directory - linuxflow and processing strategy - muleEntity Framework automatically generates a primary key - c #Docker Compose with PHP, MySQL, nginx connection problem - phpAngular JS cannot display Social Media widgets when changing page / route - angularjsAll Articles