Jquery and multiple forms on a page

前端 未结 4 2010
[愿得一人]
[愿得一人] 2021-01-17 06:30

I have a several forms that are output from a database on the same page. It works fine when I don\'t use ajax. When I use Jquery it will only work for the first form. Could

4条回答
  •  南笙
    南笙 (楼主)
    2021-01-17 06:59

    All the forms and the fields have the same name / class. So when you do

    var hardSoft = $('.hardSoft').val();
    

    you only get the value of the first element with class hardSoft.


    You can get the "parent" form element with .closest() and use .serialize() to create the data string:

    $('.updateSubmit').live('click', function() {
    
        var $form = $(this).closest('form'); // get the form element this button belongs to
        var theData = $form.serialize(); // generates the data string
    
        $.ajax ({
           type: 'POST',
           url: '/updateGrab.php',
           data: theData,
           success: function(aaa) {
             // append return data to the current form
             $form.append('
    '+aaa+'
    '); } // end success }); // end ajax return false; )};

提交回复
热议问题