【问题标题】:HTML table and inner button calling a Javascript function调用 Javascript 函数的 HTML 表格和内部按钮
【发布时间】:2018-06-28 22:58:52
【问题描述】:

我在 file1.php 中创建了一个这样的表:

<table id="myTable" style="width:100%;">
    <tr>
      <td><?php echo $string1; ?></td>
      <td><?php echo $string2; ?></td>
      <td><?php echo $string3; ?></td>
      <td><?php echo $string4; ?></td>
      <td><?php echo $string5; ?></td>
    <tr>

  <?php
  if(file_exists($filesDir))
  {
     foreach (new DirectoryIterator($filesDir) as $file)
     {
        if((htmlentities($file) !== ".") && (htmlentities($file) !== ".."))
        {
          /* get the file name, creation date and size */
          $filename = htmlentities($file);
          $filecdate = date("d-m-Y H:i:s", filectime($filesDir.$file));
          $filesize = filesize($filesDir.$file) /(pow(1024, 2));
        ?>
          <tr>
            <!-- print filename -->
            <td><?php echo $filename; ?></td>
            <!-- print file creation date -->
            <td><?php echo $filecdate; ?></td>
            <!-- print filesize -->
            <td><?php echo number_format($filesize, 2, '.', '\'')." MB"; ?></td>

            <!-- print button #1 -->
            <td> <?php echo "<input type=\"button\" value=\"D\" onClick=\"downloadFile($filename)\"/>"; ?> </td>

            <!-- print button #2 -->
            <td> <?php echo "<input type=\"button\" value=\"X\" onClick=\"eraseFile($filename)\"/>"; ?> </td>
          </tr>
        <?php
        }
     }
  }
  ?>
  </table>

我在另一个js文件中写了一个Javascript函数:

function downloadFile(filename)
{
  $("#activity").html("<img src=\"imagenes/indicator.gif\" style=\"left: 590px;top: 74px;margin: auto;position: absolute;width: 32px;height: 32px;\" />");

    window.location.href = "file3.php?filename=" + filename;
}

最后,在一个名为 file3.php 的 PHP 文件中,我使用通过 downloadFile 函数获得的 filename 值,如下所示:

if(isset($_GET["filename"]))
{
  $filename = $_GET["filename"];

  if(file_exists($filesFolder.$filename))
  {
    set_time_limit(0);
    ob_start();
    //header("Content-Type: application/force-download");
    header("Content-Type: application/octet-stream");
    header("Content-Type: application/download");
    //header('Content-Description: File Transfer');
    header('Content-Disposition: attachment; filename='.basename($filename));
    //header('Content-Transfer-Encoding: binary');
    header('Expires: 5');
    header('Cache-Control: must-revalidate');
    //header('Pragma: public');
    header('Content-Length: ' . filesize($filesFolder.$filename));
    readfile($filesFolder.$filename);
    ob_end_flush();
    exit;
  }

但是当我点击 file1.php 中的按钮时,Javascript 函数没有被执行。 你能帮忙理解我哪里错了吗?

【问题讨论】:

  • 查看由 php.ini 生成的标记。我相信这会告诉你你的错误。
  • 如果您将问题中的代码修改为重现问题所需的绝对最小值,这对您和我们都有帮助。
  • 只是一个建议,您可以使用 PHP 仅回显 $filename 变量而不是整个按钮 HTML,因此您不必处理转义所有这些引号。

标签: javascript php html-table


【解决方案1】:

您的 $filename 变量将是一个字符串。 所以尝试添加引号。

onClick=\"downloadFile('$filename')\"

我不确定这是唯一的错误,但我确定这是一个。

【讨论】:

  • 您不能在 HTML 化妆中注释掉这样的引号。正确的想法,错误的引用。
【解决方案2】:

我建议你尽量不要使用php来打印html标签。

代替:

<td><?php echo "<input type=\"button\" value=\"D\" onClick=\"downloadFile($filename)\"/>"; ?> </td>

尝试使用:

<td><input type="button" value="D" onClick="downloadFile('<?php echo $filename;?>');"/></td>

同样在第一行,我想您可能想使用

而不是 。

同时使用 和

标签(不带空格)。

【讨论】:

    猜你喜欢
    • 2010-12-29
    • 1970-01-01
    • 2021-02-17
    • 2014-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多