How to disable a submit button when AJAX request is in progress and enable it after receiving success AJAX response?

夙愿已清 提交于 2019-11-30 20:34:20

for disable

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

for enable

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

like below in your function

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
    }
  });
}

Something like this would do what you want. This is global, for the whole page, for any ajax request.

$(document).ajaxStart(function() {
   $("#search").prop('disabled', true);
}).ajaxStop(function() {
   $("#search").prop('disabled', false);
});

I would disable the button before ajax call and enable it again on success callback, any problem in doing somethig like below ?

 function GetProductByManufacturerID(value) { 
  $('#search').addAttr('disabled');
  $.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);
    $('#search').removeAttr('disabled');
     }
   });
   }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!