簡體   English   中英

如何在ajax過程中顯示加載動畫和禁用按鈕

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

我正在使用 ajax 將按鈕的 id 發送到 test.php,但我被困在如何在 ajax 過程中禁用按鈕和顯示處理圖像 gif 的步驟,

當我使用我的代碼時,它只會禁用和隱藏第一個表格行按鈕,而不是單擊的特定按鈕

我的代碼是這樣的

     <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 文件是

 <?php

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

首先,您需要刪除您在 PHP 循環內的 HTML 中創建的任何id屬性,因為這會創建無效的重復項。 而是將它們更改為類。

其次,使用不顯眼的事件處理程序附加您的事件,而不是內聯事件處理程序,因為它們已經過時且實踐不佳。 當您使用 jQuery 時,這可以通過click()on()方法來完成。

最后,在那個不顯眼的事件處理程序中,您可以使用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();
    }
  });
});

如果您正在使用 ajax,那么(使其盡可能簡單)。

將您的加載 gif 圖像添加到 html 並使其隱藏(現在使用 html 本身的樣式,您可以將其添加到單獨的 CSS):-

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

單擊按鈕時顯示圖像並在成功功能時再次隱藏它。

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

確保你也隱藏了 ajax 錯誤回調中的圖像,以確保即使 ajax 失敗也隱藏 gif。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM