简体   繁体   English

如何在ajax过程中显示加载动画和禁用按钮

[英]how to show loading animation and disable button during ajax process

i am using ajax to send id of button to test.php,But i am stuck on the step how to disable button and show processing image gif during ajax process ,我正在使用 ajax 将按钮的 id 发送到 test.php,但我被困在如何在 ajax 过程中禁用按钮和显示处理图像 gif 的步骤,

when i use my code it only disable and hide on first table row button not specific button that was clicked当我使用我的代码时,它只会禁用和隐藏第一个表格行按钮,而不是单击的特定按钮

my code is this我的代码是这样的

     <tbody>

   <?php

   $letter=mysqli_query($con,"SELECT * FROM letters order by id DESC");



             if(mysqli_num_rows($letter)>0){
        while($rows_letter=mysqli_fetch_array($letter))

        {
$id=$rows_letter['id'];
$subject=$rows_letter['subject'];
$status=$rows_letter['status'];
?>

    <tr>
      <th class="text-center" scope="row">1</th>
      <td class="text-center"><?php echo $subject ;?></td>
      <td class="text-center"><?php if($status == 1){echo '<mark style="background-color: #5cb85c; color:white;" > Successfully Sent </mark>'; }else{ echo '<mark  style="background-color:#f0ad4e; color:white;"> Not  Sent Yet </mark>';}?></td>
      <td><button type="button" class="btn btn-info btn-sm btn-block">
      <span class="fa fa-pencil-square-o"></span> Edit</button></td>
      <td><button type="button" class="btn btn-danger btn-sm btn-block"> <span class="fa fa-trash-o"></span>  Move To Trash</button></td>
      <td>
      <img src="https://svc.opushealth.com/balcoltrasavings/img/Processing.gif" id="img" style="display:none"/>
      <button type="button" onclick="startsend(<?php echo $id;?>);"
      id="id"  value="<?php echo $id;?>"class="btn btn-success btn-sm btn-block">
      <span class="fa fa-paper-plane-o"></span> Send To All</button></td>
    </tr>


      <?php

        }

             }      

          ?>
     </tbody>
       <script type='text/javascript'>

    //AJAX function
   function startsend(id) {
       $('#img').show(); 
       $("#id").attr("disabled", true);
      $.ajax({
        type: "POST",
        url: "test.php",
        data:{ id: id },
        success: function(msg){
        alert( "Button Id is " + msg );
         $('#img').hide();

        }
      });
    }

</script>

test.php file is test.php 文件是

 <?php

if(isset($_POST['id'])){
$id = $_POST['id'];}
///rest process
?>

Firstly you need to remove any id attributes you create in your HTML within the loop in your PHP, as this will create duplicates which is invalid.首先,您需要删除您在 PHP 循环内的 HTML 中创建的任何id属性,因为这会创建无效的重复项。 Instead change them to classes.而是将它们更改为类。

Secondly, attach your events using unobtrusive event handlers, not inline ones as they are outdated and bad practice.其次,使用不显眼的事件处理程序附加您的事件,而不是内联事件处理程序,因为它们已经过时且实践不佳。 As you're using jQuery this can be done with the click() or on() methods.当您使用 jQuery 时,这可以通过click()on()方法来完成。

Lastly, within that unobtrusive event handler you can use the this keyword to refer to the element which raised the event and modify it as required.最后,在那个不显眼的事件处理程序中,您可以使用this关键字来引用引发事件的元素并根据需要对其进行修改。 Try this:尝试这个:

<img src="https://svc.opushealth.com/balcoltrasavings/img/Processing.gif" class="img" style="display: none" />
<button type="button" value="<?php echo $id;?>" class="id btn btn-success btn-sm btn-block">
  <span class="fa fa-paper-plane-o"></span> 
  Send To All
</button>
$('button.id').on('click', function() {
  var $btn = $(this).prop("disabled", true);
  var $img = $btn.prev('.img').show(); 

  $.ajax({
    type: "POST",
    url: "test.php",
    data: { id: $btn.val() },
    success: function(msg) {
      console.log("Button Id is " + msg);
      $img.hide();
    }
  });
});

If you are using ajax then (making it as simple as possible).如果您正在使用 ajax,那么(使其尽可能简单)。

Add your loading gif image to html and make it hidden (using style in html itself now, you can add it to separate CSS):-将您的加载 gif 图像添加到 html 并使其隐藏(现在使用 html 本身的样式,您可以将其添加到单独的 CSS):-

<img src="path\to\loading\gif" id="img" style="display:none"/ >

Show the image when button is clicked and hide it again on success function.单击按钮时显示图像并在成功功能时再次隐藏它。

$('#buttonID').click(function(){
  $('#img').show(); //<----show here
  $.ajax({
    ....
   success:function(result){
       $('#img').hide();  //<--- hide again
   }
}

Make sure you hide the image on ajax error callbacks too to make sure the gif hides even if the ajax fails.确保你也隐藏了 ajax 错误回调中的图像,以确保即使 ajax 失败也隐藏 gif。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM