一尘不染

如何在AJAX请求进行时禁用提交按钮,并在收到成功的AJAX响应后启用它?

ajax

我正在遵循HTML代码:

<form action="view_rebate_master.php" method="post">
  <div class="form-group">
    <label for="company_name" class="col-lg-12">Manufacturer</label>
    <div class="col-lg-12">
      <select id="company_id" class="form-control" onchange="GetProductByManufacturerID(this.value)" name="company_id">
        <option selected="selected" value="">All Manufacturers</option>
        <option value="40">Test</option>
        <option value="42">RK</option>
        <option value="49">Blue Nun</option>
        <option value="58">Unique Imports</option>
        <option value="59">Pernod Ricard</option>
        <option value="77">Smoking Loon</option>
        <option value="78">Beringer</option>
      </select>
    </div>
  </div>
  <div class="col-xs-4">
    <div class="form-group">
      <label for="product_id" class="col-lg-12">Product Name</label>
      <div class="col-lg-12">
        <select id="product_id" class="form-control" name="product_id">
          <option selected="selected" value="">All Products</option>
          <option value="12">Riesling</option>
          <option value="24">Superio Vodka</option>
          <option value="32">Heineken</option>
          <option value="33">Strong Bow</option>
          <option value="34">Grocery</option>
          <option value="35">Ruler</option>
          <option value="36">Glass</option>
          <option value="37">Brown Bread</option>
          <option value="38">White Bread</option>
          <option value="55">Cabernet Sauvignon</option>
        </select>
      </div>
    </div>
  </div>
  <div class="col-lg-12">   
    <div class="col-xs-5">
      <div class="form-group">
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<button type="submit" class="btn btn-primary" name="search" id="search">Search Rebates</button>
    </div>
  </div>
</div>

AJAX jQuery代码如下:

function GetProductByManufacturerID(value) { 
  $.ajax({
    type: "POST",
    url: "add_rebate_by_quat_volume.php",
    data: { manufacturer_id: value, op:"" },
    beforeSend: function() { 
      $("#product_id").html('<option> Loading ...</option>');
    },
    success:function(data){ 
      $("#product_id").html('');
      $("#product_id").append(data);
    }
  });
}

我想通过更改选择控件(用于制造商选择的选择控件)的值来进行AJAX函数调用时使提交按钮禁用,并且应该禁用它,直到收到AJAX成功响应为止。当收到AJAX成功响应时,用户应该能够单击“提交”按钮。如何实现呢?提前致谢。


阅读 142

收藏
2020-07-26

共1个答案

一尘不染

对于 禁用

 $("#search").prop('disabled', true);

使

 $("#search").prop('disabled', false);

像下面的功能

function GetProductByManufacturerID(value) { 
  $.ajax({
    type: "POST",
    url: "add_rebate_by_quat_volume.php",
    data: { manufacturer_id: value, op:"" },
    beforeSend: function() { 
      $("#product_id").html('<option> Loading ...</option>');
      $("#search").prop('disabled', true); // disable button
    },
    success:function(data){ 
      $("#product_id").html('');
      $("#product_id").append(data);
      $("#search").prop('disabled', false); // enable button
    }
  });
}
2020-07-26