Javascript 将表行元素传递给js
我有一个从数据库加载元素的引导表。 我有一个js函数从数据库中删除一个表行,所以我在表的每一行中添加了一个按钮Javascript 将表行元素传递给js,javascript,twitter-bootstrap,html-table,Javascript,Twitter Bootstrap,Html Table,我有一个从数据库加载元素的引导表。 我有一个js函数从数据库中删除一个表行,所以我在表的每一行中添加了一个按钮 <table id="datatable-responsive" class="table table-striped table-bordered dt-responsive nowrap" cellspacing="0" width="100%"> <thead>
<table id="datatable-responsive" class="table table-striped table-bordered dt-responsive nowrap" cellspacing="0" width="100%">
<thead>
<tr>
<th>id</th>
<th>client</th>
</tr>
</thead>
<tbody>
<?php
require("config.php");
// Create connection
$conn = new mysqli($host, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM users";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while ($row = $result->fetch_row()) {
$user_id=$row[0];
$user_name=$row[1];
?>
<tr>
<!--here showing results in the table -->
<td><?php echo $user_id; ?></td>
<td ><?php echo $user_name; ?></td>
<td>
<input type="button" class="btn btn-danger btn-xs delete" value="Cancella sin id" onclick="deleteFunction()"/>
</td>
</tr>
</tbody>
</table>
TD应该是这样的
<td id="user_name_<?=$user_id?>" onClick="deleteFunction(<?=$user_id?>)"><?=$user_name?></td>
function deleteFunction(user_id) {
var username = document.getElementById('user_name_'+user_id).innerHTML;
}
如果在循环时将按钮添加到每一行,则可以将id添加到按钮的
onclick=“functionDelete(id)”
的onclick
中,这将始终获得列表中的第一个。无论单击哪个按钮!您不应该有具有相同id的元素吗?你不应该每次都有不同的id吗?比如用户名1、用户名2等等。如果是这样,你需要的就是将你的评论和我的评论结合起来。但是如果你有id,为什么你需要这个名称。。。ID应该始终是唯一的,因为名称可以相同。。。。如果您想在数据库中引用它,ID是最好的方法。对于OP,请将您的答案更改为编辑后的原始答案以反映当前状态
function deleteFunction(user_id) {
var username = document.getElementById('user_name_'+user_id).innerHTML;
}