【问题标题】:change button text in JS/jQuery upon process completion in php在 php 中完成进程后更改 JS/jQuery 中的按钮文本
【发布时间】:2019-06-13 18:48:08
【问题描述】:

我正在编写如下所示的调用 convert.php 脚本的 jQuery/AJAX 代码。

   jQuery(document).ready(function($)
   {
    $('.go-btn').click(function()
    {   
        let target = $(this).attr('data-id'),
            spanEl = $('.file-name[data-id='+ target +']');

        $.ajax({
            url: 'convert.php',
            type: 'POST',
            data: {id: target}, //change to what you want
            success: function(res)
            {
                console.log("Tested");
            },
            error: function(res)
            {

            }
        })
    })  
   });  

convert.php 脚本中将 mp4 转换为 mp3。转换完成后,控制台上会打印 Tested

问题陈述:

我想知道我应该在上面的 jQuery/AJAX 代码中进行哪些更改,以便在转换完成后,属于下面 HTML 代码的按钮文本更改为 Completed

上面的 jQuery/AJAX 代码在按钮点击时被调用。这是属于该按钮的 HTML 代码的 sn-ps:

HTML 代码:

  <?php  foreach ($programs as $key => $program) {  ?> 
       <tr data-index="<?php echo $key; ?>">
          <td><input type="submit" name="go-button" value="Go" data-id="<?php echo $key; ?>" ></input></td>
       </tr>
  <?php }?>

【问题讨论】:

  • 为什么不使用 jQuery 更新按钮文本 success: $('.go-btn').val('Completed');
  • 你要我在哪里更新这个?
  • success 内 - 与您将“已测试”写入控制台的位置相同。
  • 喜欢这个? success: function(res) { $('.go-btn').val('Completed'); },
  • 成功了,感谢@MER 的帮助我有一个小问题。上面的 HTML 代码有很多行,每一行都有 Go 按钮。完成单个行的过程后,Go 文本将在所有行中更改为 Completed,这不是我想要的。 Go 文本应更改为仅完成其所属的行。

标签: javascript php jquery ajax


【解决方案1】:

这只是捕获被点击的按钮的问题:

jQuery(document).ready(function($) {
  $('.go-btn').click(function() {
    let target = $(this).attr('data-id'),
      spanEl = $('.file-name[data-id=' + target + ']');
    // Capture the element that received the event
    let btn = this;
    $(btn).val("Converting").prop('disabled', true).css({
      'text-align': 'center',
      'border-color': '#000',
      'width': '120px'
    });
    $.ajax({
      url: 'https://api.myjson.com/bins/80dex',
      method: 'GET', // Change this back to POST
      data: {
        id: target
      }, //change to what you want
      success: function(res) {
        $(btn).val("Completed")
      },
      error: function(res) {
        $(btn).prop('disabled', false).val("Go").removeAttr('style');
      }
    })
  })
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>
<tr data-index="1">
  <td>
    <input type="submit" name="go-button" class="go-btn" value="Go" data-id="1"></input>
  </td>
</tr>
<tr data-index="2">
  <td>
    <input type="submit" name="go-button" class="go-btn" value="Go" data-id="2"></input>
  </td>
</tr>
<tr data-index="3">
  <td>
    <input type="submit" name="go-button" class="go-btn" value="Go" data-id="3"></input>
  </td>
</tr>

【讨论】:

  • 为什么加let btn = this;
  • @john 因为它是被点击的特定按钮。如果您尝试在回调中使用thisthis 将指向请求。 Javascript 就是这么奇怪。你可以了解更多in this articlethis book
  • 有意义,我们可以同时使用GETPOST
  • @john 你可以在同一个处理程序中完成,我更新了答案
  • @john 如果您通过事先禁用按钮在 php 端处理该问题怎么办?如果您正在寻找实时更新,我想您可能会改用 WebSockets。如果不了解您的架构,很难说。
猜你喜欢
  • 1970-01-01
  • 2023-03-27
  • 1970-01-01
  • 2019-10-25
  • 2015-11-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多