【发布时间】:2021-09-30 14:32:05
【问题描述】:
我有两个文件,load-request.php 和 load-data.php,当我单击删除按钮获取其值时,load-request.php 中的删除按钮返回一个未定义的值。 请告知,为什么我没有得到价值。
加载请求.php
<form>
<table class="table" id="main" border="0" cellspacing="0">
<tr>
<td id="table-load">
<input type="button" id="load-button" value="Load Data">
</td>
</tr>
<table class="table" width="55%">
<tr>
<td id="table-data"></td>
</tr>
</table>
</td>
</tr>
</table>
<script>
$(document).ready(function() { //ajax to load the table
$("#load-button").on("click", function() {
$.ajax({
url: "load-data.php",
type: "POST",
dataType: "text",
success: function(data) {
$("#table-data").html(data);
}
});
});
$(document).on("click", ".delete-btn", function() {
console.log($(this).find("data-id"));
var pn = $(this).attr('value');
alert(pn);
});
});
</script>
</form>
</body>
</html>
加载数据.php
<?php
while ($row = mysqli_fetch_assoc($result)) {
$output .= "<tr><td>{$row["pname"]}</td><td>{$row["src"]}</td><td>{$row["dst"]}</td><td>{$row["ports"]}</td><td>{$row["inzone"]}</td><td>{$row["outzone"]}<td><button Class='delete-btn' **data-id'{$row["pname"]}**'>Delete</button></td></tr>";
}
$output .= "</table>";
echo $output;
mysqli_close($conn);
?>
【问题讨论】:
-
欢迎来到 Stack Overflow!从你的 php 看你的 HTML 是什么样的?您似乎缺少
=的data-id={$row["pname"]} -
我想你错过了一个 =
-
添加了这个 = 但结果仍然相同,从处理数据库连接和事务的 load-data.php 中,我可以在 load-request.php 中获取表中的所有记录,唯一的问题是在 load-data.php 中创建的按钮不返回值。 while ($row = mysqli_fetch_assoc($result)) { $output .= "
{$row["pname"]} {$row["src"]} td> {$row["dst"]} {$row["ports"]} {$row["inzone"]} {$row["outzone"]} .
标签: javascript php jquery ajax