【问题标题】:Append/add a <div> tag inside a table row when row button is clicked jquery/php单击行按钮时,在表格行内追加/添加 <div> 标签 jquery/php
【发布时间】:2020-07-15 15:54:09
【问题描述】:

我有一个带有文件夹路径的表格,每个表格行内都有一个按钮。当在一行中单击按钮时,路径会发送到一系列 php 文件(ajax.php、ajax2.php、ajax3.php)。 ajax3.php 完成后,会创建一个 pdf 报告,并且 pdf_report.php 会显示该报告。

问题:我找不到在单击按钮的行内显示 pdf 报告的方法。当我点击“运行”时,pdf 报告会显示在所有行上。

我的代码如下:

search.php

        <?php
        ...
        while($row = $result->fetch_assoc()){
            $field1name = $row["test_id"];
            $field2name = $row["path"];
            $field3name = $row["video1_path"];
            $field4name = $row["video2_path"];
            $field5name = $row["video3_path"];
            $field6name = $row["video4_path"];

          echo "<tr>
                  <td> ".$field1name." </td>
                  <td> ".$field2name." </td>
                  <td> ".$field3name." </td>
                  <td> ".$field4name." </td>
                  <td> ".$field5name." </td>
                  <td> ".$field6name." </td>

                  <td><div>
                        <button class='edit' id='" . $row['test_id'] . "'>Run</button>
                      </div></td>


                  <td><div class= 'result' id='" . $row['test_id'] . "'>
                    <p></p>
                  </div></td>

                </tr>";
        }
      }else {
        echo '<span style="color:#ff0000;text-align:center;">No Test ID Selected!</span>';
      }
    }


    // Close connection
    mysqli_close($conn);
  ?>
  </table>
</div><br>

<div style="overflow-x:auto;">
<table id=test_data>
  <tr>
    <th>Progress</th>
    <th>Progress Status</th>
  </tr>
  <tr>
    <td><div><progress id='progBar' value='0' max='100'></progress></div></td>
    <td><div><p id='progress-text'></p></div></td>
  </tr>
</table>
</div>


<!--Uses jquery to run 3 scripts and displays it in a progress bar-->
<script>
$(document).on('click', '.edit', function(){
  //set cookie value to 'path'
  var fcookie='mycookie';
  var test_id = $(this).attr('id');
  if(test_id) {
    var path = $(this).closest('tr').find("td:nth-child(2)").text();
  }

  document.cookie='fcookie='+path;

    //Start of 1st script
      $.ajax({
        url: "ajax.php",
        type:"POST",
        success: function(data) {
          //alert("File 1 Completed")

            $("#progress-text").text("Executing file 1");
            $('#progBar').val(25);

            //Start of 2nd script
            $.ajax({
              url: "ajax2.php",
              type:"POST",
                success: function(data2) {
                  //alert("File 2 Completed")
                  $("#progress-text").text("Executing file 2");
                  $('#progBar').val(50);

                  //Start of 3rd script
                  $.ajax({
                    url: "ajax3.php",
                    type:"POST",
                      success: function(data3) {
                        //alert("File 2 Completed")
                         $("#progress-text").text("Complete");
                         $('#progBar').val(100);


                        $(this).closest("tr").find("td:nth-child(8)").load("pdf_report.php");

                      }
                  })
                }
            })
          }
    })
    return false;
   //});
});



</script>

pdf_report.php

...
<style>
.report {
  color: black;
  border: 2px solid #008CBA;
  border-radius: 8px;

}
</style>

<!-- Opens the pdf report in a new tab "_blank" -->
<a href= "<?php echo $path.$pdf; ?>" class="report w3-button" target="_blank">Report</a>

【问题讨论】:

  • 欢迎来到 Stack Overflow!我们希望你喜欢这里。 为什么this 用引号引起来? 请参加导览:stackoverflow.com/tour 并提供一个可重现的最小示例:stackoverflow.com/help/minimal-reproducible-example
  • 同意@PeterKA,请使用 $(this) 而不是 $('this') 如果这不起作用,请声明一个 $(this) 变量并在您的第三个下使用它阿贾克斯
  • 编辑到 $(this) 并没有什么不同。还有什么建议吗?
  • When the button is clicked inside a row - 我们需要查看调用 ajax 的 javascript/jQuery... 这很可能是问题所在。
  • 嗨@cssyphus,它从这里开始 $(document).on('click', '.edit', function(){

标签: php html jquery


【解决方案1】:

我认为这是一个范围问题。 $(this) 不再指代 $.ajax() 函数中的同一事物。

试试这个:

//Start of 1st script
var $this = $(this);  <=================== added this line, and modded one 30 below
$.ajax({
  url: "ajax.php",
  type: "POST",
  success: function(data) {
    //alert("File 1 Completed")

    $("#progress-text").text("Executing file 1");
    $('#progBar').val(25);

    //Start of 2nd script
    $.ajax({
      url: "ajax2.php",
      type: "POST",
      success: function(data2) {
        //alert("File 2 Completed")
        $("#progress-text").text("Executing file 2");
        $('#progBar').val(50);

        //Start of 3rd script
        $.ajax({
          url: "ajax3.php",
          type: "POST",
          success: function(data3) {
            //alert("File 2 Completed")
            $("#progress-text").text("Complete");
            $('#progBar').val(100);

            vv <=================================== modded $(this) to $this
            $this.closest("tr").find("td:nth-child(8)").load("pdf_report.php");

          }
        })
      }
    })
  }
})

【讨论】:

  • 感谢您抽出宝贵的时间回答我的问题。成功了!
  • 你好@cssyphus,很抱歉再次打扰,但我有一个简单的问题。如果我想保持表格行内的
    始终显示,即使页面被刷新,我怎么能做到这一点?我使用了本地存储,但它不起作用。你有什么建议
  • 嗨@ian,很高兴再次与您交谈。恐怕我不明白你的询问。你在说什么div?您能否提出一个新问题并创建一个演示该问题的代码示例?我只需要确切地知道您在问什么,以便我了解问题所在。 非常感谢!
  • 谢谢。当你有机会时,这就是问题所在。 stackoverflow.com/questions/63123713/…
  • 你有机会看到这个问题吗? @cssyphus
猜你喜欢
相关资源
最近更新 更多
热门标签