简体   繁体   English

将HTML的deleteRow()与PHP生成的表相关联

[英]Associating HTML's deleteRow() with a table generated by PHP

I have the following PHP code to generate a table where there are rows whose columns have the following attributes: "id", "lastname", and "firstname". 我有以下PHP代码来生成一个表,该表中的行的列具有以下属性:“ id”,“ lastname”和“ firstname”。 I want to add a button for each of the rows that deletes the respective row and have this line to attempt to do so: echo '<td><input type="button" value="Delete" onclick="deleteRow('+$i+')"></td>'; 我想为每行添加一个按钮,以删除相应的行,并尝试执行以下操作: echo '<td><input type="button" value="Delete" onclick="deleteRow('+$i+')"></td>'; in the following code. 在以下代码中。 However, it's printing the value of $i (1,2,3,4,... which are the row numbers I want associated with the buttons) instead of having delete buttons dedicated to each row (with the follow code below, it doesn't create any buttons unless I specifically remove the '+$i+' part). 但是,它打印的是$ i的值(1,2,3,4,...,这是我要与按钮关联的行号),而不是每行都有专用的删除按钮(下面是以下代码)除非我专门删除'+$i+'部分,否则不会创建任何按钮。 What is the issue? 有什么问题

<table>
                <tr>
                <td></td>
                <td>id</td>
                <td>Last Name</td>
                <td>First Name</td>
                </tr>
                <?php
            $i = 1;
            foreach ( $names['result'] as $a ){       // $a is an array    
                echo '<tr>';
                echo '<td><input type="button" value="Delete" onclick="deleteRow('+$i+')"></td>';   // issue is here
                echo '<td>' . $q['id'] . '</td>';
                echo '<td>' . $q['lastName'] . '</td>'; 
                echo '<td>' . $q['firstName'] . '</td>';
                echo '</tr>';
            }
?>
</table>

The . . is the string concatenation operator in php language whereas the + is a concatenation operator in javascript language. php语言中的字符串串联运算符,而+javascript语言中的串联运算符。

You should use onclick="deleteRow('.$i.')" 您应该使用onclick="deleteRow('.$i.')"

And then, just before ending the loop, just use a $i++; 然后,在结束循环之前,只需使用$i++;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM