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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19182199/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 23:23:39  来源:igfitidea点击:

to submit a form among multiple forms using jquery-ajax

ajaxformsjquerymultiple-forms

提问by Harika Choudary Kanikanti

i have multiple forms with same class name

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

<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>

Then how can i submit a single form among this n number of forms i tried using

那么我如何在我尝试使用的这 n 个表单中提交一个表单

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

                  }
          });
         e.preventDefault();
     });
  });

But it is submitting always 1st form among the n-forms. How can submit a random form among the n-forms. May any one help me please.

但它总是提交 n 种形式中的第一种形式。如何在n个表单中提交一个随机表单。请任何人帮助我。

回答by Reinstate Monica Cellio

You need to reference the form with thiswhen you serialize it...

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

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

回答by Hiral

Use this keyword. It will submit the form on which the event has been triggered.

使用这个关键字。它将提交触发事件的表单。

why are u using location.reload?

你为什么使用location.reload?

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

回答by Pankaj S. Mahajan

 You can try this...

 function submitForm(){
document.formName.action="actionName";
document.formName.submit();
 }


 <form method="post" name="formName" enctype="multipart/form-data">
  ....
 <input type="button" onclick="submitForm()"/>
 <form>