javascript - 在 foreach 循环内将输入文本复制到另一个文本?

标签 javascript php foreach key getelementbyid

此页面生成一个包含路由的表,并且在每个“tr”的末尾有一个按钮,该按钮应将每行中这 2 个字段内的文本分别复制到 Name3 和 Name 4 输入。

但是当我点击时,没有任何反应,我认为 getElementById("Name1_".index) 使洞代码无效,但如何正确使其正确?

<script type="text/javascript">
          function copyTextValue(key) {
                        var index = key;
                        var text1 = document.getElementById("Name1_".index).value;
                        var text2 = document.getElementById("Name2_".index).value;
                        document.getElementById("Name3").value = text1;
                        document.getElementById("Name4").value = text2;
                        }
</script>

         <input type="text" id='Name3' />
         <input type="text" id='Name4' />

        <?php

        $get_rotas = file_get_contents('routes.txt');

        $array = explode("\r\n", $get_rotas);

        foreach($array as $key=>$rota) {

            //Route(rota) Example: TRM J169 BLH V16 BXK|3666

            $rota_distance = explode("|", $rota);

            echo "<tr bordercolor='#FFFFFF'>";
            echo "<td><input type=\"text\" id=\"Name1_$key\" value='" . $rota_distance[0] . "'/></td>";
            echo "<td><input type=\"text\" id=\"Name2_$key\" value='" . $rota_distance[1] . "'/></td>";

            echo "<td><input type=\"button\" onclick=\"copyTextValue($key);\" value=\"Copy\" ></td>";

            echo "</tr>";

            } 

            ?>

最佳答案

要在 JavaScript 中连接字符串,请使用 + 而不是 .

关于javascript - 在 foreach 循环内将输入文本复制到另一个文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24222781/

相关文章:

javascript - iOS 上的 Facebook 浏览器 : problems with localStorage

JavaScript 可能发生意外迭代

php - Woocommerce 可选产品属性

c# - 即使MSDN另有建议,为什么Parallel.ForEach比AsParallel()。ForAll()快得多?

javascript - JavaScript 有元组吗?

javascript - 用 jquery 删除最后一个 Accordion

PHP 变量未显示在页面上

php - php中的ipad方向检测?

r - Foreach combine error, rbind all variables should have the same length...但他们这样做 [R]

Perl - 如果需要很长时间,在按键时中断系统/反引号命令