dtdvbf37193 2018-05-28 03:16
浏览 54

复制按钮不读取第二个值

I want to make the copy button copy the value of user, but the problem is the button copied only the value of first user. When move to second user, it remain copied the valued of the first user. Here are my code.

<?php
    require_once('connection.php');
    $sql = "SELECT ftname, menu, longlan FROM 
    ownerinfo";
    $result = $con->query($sql);

    if ($result) {
        // output data of each row
        while($row = $result->fetch_assoc()) {
            $a=1;
            $long[$a]=$row["longlan"];

            echo "Food Truck: " . $row["ftname"]. " 
            <br>Menu: " . $row["menu"].  " 
            <br>Longitude Latitude: <input id='he ' 
            value='".$long[$a]."'></input><button 
            onclick='myFunction()'>Copy</button><br>
                                <a 
            href='https://maps.google.com/'>Maps</a> 
            <hr>";

            $a=$a+1;
        }          
    } else {
        echo "0 results";
    } 
?>

<script>
  function myFunction() {
  var copyText = document.getElementById("he");
  copyText.select();
  document.execCommand("copy");
  alert("Copied the text: " + copyText.value);
  }
  • 写回答

1条回答 默认 最新

  • 普通网友 2018-05-28 03:20
    关注

    Considering your loop outputs an ID, your JavaScript will only ever be able to target the first instance of that ID (as multiple IDs are considered invalid markup).

    To correct this, output classes from your PHP instead:

    <input class='he' value='".$long[$a]."'>
    

    And loop over each of the classes in your JavaScript:

    const copyText = document.getElementsByClassName("he");
    for (let i = 0; i < copyText.length; i++) {
      copyText[i].select();
      document.execCommand("copy");
      alert("Copied the text: " + copyText[i].value);
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端