使用jquery-ajax在多个表单中提交表单

Har*_*nti 10 forms ajax jquery multiple-forms

我有多个具有相同类名的表单

<form >
  <input type="hidden" value="<%=ids%>" name="idd">
  <input type="hidden" value="<%=email%>" name="cby">
  <input type="text" class="cmd" name="cm" style="width:300px;" placeholder="comment">
  <input type="submit" value="" style="display:none;">
</form>
  <!-- n number of forms are  generated using while loop-->
<form>
  <input type="hidden" value="<%=ids%>" name="idd">
  <input type="hidden" value="<%=email%>" name="cby">
  <input type="text" class="cmd" name="cm" style="width:300px;" placeholder="comment">
  <input type="submit" value="" style="display:none;">
</form>
Run Code Online (Sandbox Code Playgroud)

然后,我如何在我尝试使用的n种形式中提交单一表格

   $(function () {
          $('form').on('submit', function (e) {
                $.ajax({
                 type: 'post',
                  url: 'addfr.jsp',
                  data: $('form').serialize(),
                  success: function () {
                  location.reload();

                  }
          });
         e.preventDefault();
     });
  });
Run Code Online (Sandbox Code Playgroud)

但它总是在n-forms中提交第一种形式.如何在n-forms中提交随机表格.愿任何人帮助我.

Arc*_*her 13

您需要this在序列化时引用该表单...

$(function () {
    $('form').on('submit', function (e) {
        $.ajax({
            type: 'post',
            url: 'addfr.jsp',
            data: $(this).serialize(),
            success: function () {
                location.reload();
            }
        });
        e.preventDefault();
    });
});
Run Code Online (Sandbox Code Playgroud)