doukong1901 2018-10-12 16:29
浏览 69

ajax提交多次

I have created an application in the php, I am using the bootstrap model to update user details using AJAX. Now there are two forms: first updates the user's details and adds comments to another user. When I try to submit a form to AJAX and I try to submit a form, it does not work, when i use ajaxComplete function then my form data has been submitted successfully, but at the time it submits the form multiple times at the same time.

$(document).ajaxComplete(function() {
      $("#add-remarks").on('submit', (function(e) {
        e.preventDefault();
        $.ajax({
          type: "POST",
          url: $("#add-remarks").attr("action"),
          cache: false,
          context: this,
          data: $("#add-remarks").serialize(), //only input
          success: function(data) {
            var obj = JSON.parse(data);
            $("#add-remarks")[0].reset();
            if (obj['msg'] == 'isuccess') {
              notification('insert');
            } else {
              notification('error');
            }
          }
        });
      }));
<div class="modal-body">
  <div class="row">
    <div class="col-md-12">
      <div class="card-group-control card-group-control-right" id="accordion-model">
        <div class="card">
          <form method="post" name="update-user-details" id="update-user-details" action="">
            <div class="row">
              <div class="col-md-4">
                <div class="form-group form-group-float">
                  <label class="d-block font-weight-semibold">First Name</label>
                  <input type="text" class="form-control" placeholder="First Name" name="customer_name" id="customer_name">
                </div>
              </div>
              <div class="col-md-4">
                <div class="form-group form-group-float">
                  <label class="d-block font-weight-semibold">Mobile</label>
                  <input type="text" class="form-control" placeholder="Mobile" name="mobile_number" id="mobile_number">
                </div>
              </div>
              <button type="submit" class="btn bg-blue">Save Changes</button>
            </div>
          </form>
          <form method="post" name="remarks" id="remarks" action="">
            <div class="row">
              <div class="col-md-4">
                <div class="form-group form-group-float">
                  <label class="d-block font-weight-semibold">Remarks</label>
                  <input type="text" class="form-control" placeholder="Remarks" name="remarks" id="remarks">
                </div>
              </div>
              <button type="submit" class="btn bg-blue">Save Changes</button>
            </div>
          </form>
        </div>
      </div>
    </div>
  </div>
</div>

</div>
  • 写回答

1条回答 默认 最新

  • doukoumi3389 2018-10-12 17:10
    关注

    Move this piece of code outside of your ajaxComplete() function. You are probably somehow triggering the ajaxComplete function multiple times, causing multiple event listeners for the form submit.

     $("#add-remarks").on('submit', (function(e) {
        e.preventDefault();
        $.ajax({
          type: "POST",
          url: $("#add-remarks").attr("action"),
          cache: false,
          context: this,
          data: $("#add-remarks").serialize(), //only input
          success: function(data) {
            var obj = JSON.parse(data);
            $("#add-remarks")[0].reset();
            if (obj['msg'] == 'isuccess') {
              notification('insert');
            } else {
              notification('error');
            }
          }
        });
      }));
    
    评论

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line