Javascript 使用jquery单击按钮时,如何删除特定行?
我有一个玩家数据表,每行旁边都有一个删除按钮。 单击按钮时,应该立即使用jQuery删除该特定行。到目前为止,我已经包括了我的代码。当按动按钮时,我真的被卡住了。我应该使用头函数吗Javascript 使用jquery单击按钮时,如何删除特定行?,javascript,php,jquery,mysqli,Javascript,Php,Jquery,Mysqli,我有一个玩家数据表,每行旁边都有一个删除按钮。 单击按钮时,应该立即使用jQuery删除该特定行。到目前为止,我已经包括了我的代码。当按动按钮时,我真的被卡住了。我应该使用头函数吗 index.php <!DOCTYPE html> <html> <head> <link rel="stylesheet" type="text/css" href="css/style.css"></link> &
index.php
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/style.css"></link>
<script src="js/jquery-1.11.3.min.js"></script>
<script src="js/script.js"></script>
</head>
<body>
<!-- Create a table-->
<table style width="800" border="1" cellpadding="1" cellspacing="1">
<!-- Create headings. Every row is defined as with <tr> tag-->
<!-- Create headings. Every row is defined as with <tr> tag-->
<tr>
<th>Player ID</th>
<th>Player Name</th>
<th>Player Position</th>
<th>Delete</th>
</tr>
<?php
require "php/conn.php";
//Select query
$res = $db -> query ("SELECT * FROM player");
//Set pointer to 1
$res -> data_seek(0);
//Display data using while loop
while ($row = $res -> fetch_assoc()) {
// Write html code in php using this method
echo "<div class = 'row'>";
echo "<tr>";
echo "<td>".$row['id']."</td>";
echo "<td>".$row['surname']."</td>";
echo "<td>".$row['position']."</td>";
echo "<td><button id = 'dlt_btn' type='button'>Delete</button></td>";
echo "</tr>";
echo "</div>";
}
?>
</body>
?
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/style.css"></link>
<script src="js/jquery-1.11.3.min.js"></script>
<script src="js/script.js"></script>
</head>
<body>
<!-- Create a table-->
<table style width="800" border="1" cellpadding="1" cellspacing="1">
<!-- Create headings. Every row is defined as with <tr> tag-->
<!-- Create headings. Every row is defined as with <tr> tag-->
<tr>
<th>Player ID</th>
<th>Player Name</th>
<th>Player Position</th>
<th>Delete</th>
</tr>
<?php
require "php/conn.php";
//Select query
$res = $db -> query ("SELECT * FROM player");
//Set pointer to 1
$res -> data_seek(0);
//Display data using while loop
while ($row = $res -> fetch_assoc()) {
// Write html code in php using this method
echo "<div class = 'row'>";
echo "<tr>";
echo "<td>".$row['id']."</td>";
$player_id = $row['id'];
echo "<td>".$row['surname']."</td>";
echo "<td>".$row['position']."</td>";
echo "<td><button id = 'dlt_btn' value = ". $row['id'] . " type='button'>Delete</button></td>";
echo "</tr>";
echo "</div>";
}
?>
</body>
<?php
require "php/conn.php";
$player_id = $_POST['id'];
$stmt = $db -> prepare ("DELETE FROM player WHERE id = $player_id");
$stmt -> execute();
?>
delete.php
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/style.css"></link>
<script src="js/jquery-1.11.3.min.js"></script>
<script src="js/script.js"></script>
</head>
<body>
<!-- Create a table-->
<table style width="800" border="1" cellpadding="1" cellspacing="1">
<!-- Create headings. Every row is defined as with <tr> tag-->
<!-- Create headings. Every row is defined as with <tr> tag-->
<tr>
<th>Player ID</th>
<th>Player Name</th>
<th>Player Position</th>
<th>Delete</th>
</tr>
<?php
require "php/conn.php";
//Select query
$res = $db -> query ("SELECT * FROM player");
//Set pointer to 1
$res -> data_seek(0);
//Display data using while loop
while ($row = $res -> fetch_assoc()) {
// Write html code in php using this method
echo "<div class = 'row'>";
echo "<tr>";
echo "<td>".$row['id']."</td>";
echo "<td>".$row['surname']."</td>";
echo "<td>".$row['position']."</td>";
echo "<td><button id = 'dlt_btn' type='button'>Delete</button></td>";
echo "</tr>";
echo "</div>";
}
?>
</body>
?
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/style.css"></link>
<script src="js/jquery-1.11.3.min.js"></script>
<script src="js/script.js"></script>
</head>
<body>
<!-- Create a table-->
<table style width="800" border="1" cellpadding="1" cellspacing="1">
<!-- Create headings. Every row is defined as with <tr> tag-->
<!-- Create headings. Every row is defined as with <tr> tag-->
<tr>
<th>Player ID</th>
<th>Player Name</th>
<th>Player Position</th>
<th>Delete</th>
</tr>
<?php
require "php/conn.php";
//Select query
$res = $db -> query ("SELECT * FROM player");
//Set pointer to 1
$res -> data_seek(0);
//Display data using while loop
while ($row = $res -> fetch_assoc()) {
// Write html code in php using this method
echo "<div class = 'row'>";
echo "<tr>";
echo "<td>".$row['id']."</td>";
$player_id = $row['id'];
echo "<td>".$row['surname']."</td>";
echo "<td>".$row['position']."</td>";
echo "<td><button id = 'dlt_btn' value = ". $row['id'] . " type='button'>Delete</button></td>";
echo "</tr>";
echo "</div>";
}
?>
</body>
<?php
require "php/conn.php";
$player_id = $_POST['id'];
$stmt = $db -> prepare ("DELETE FROM player WHERE id = $player_id");
$stmt -> execute();
?>
在此上下文中,您应该使用类vs ID,但可以使用$(this)定位单击的元素,并通过.parent()jquery方法遍历/删除。这纯粹是为了从DOM中删除行,您需要执行一个ajax请求来告诉服务器要从数据库中删除哪一行。在这种情况下,您应该将row ID属性添加到html中,并将其与AJAX请求一起发送 可以将行ID作为属性附加到按钮中,如
使用DOM操作:
$(document).ready(function(){
$("#dlt_btn").click(function () {
var deleteRow = $(this)
deleteRow.parents('.row').fadeOut(200);
});
})
如果您想从数据库中删除。当函数同时运行时,触发php中的另一行代码
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/style.css"></link>
<script src="js/jquery-1.11.3.min.js"></script>
<script src="js/script.js"></script>
</head>
<body>
<!-- Create a table-->
<table style width="800" border="1" cellpadding="1" cellspacing="1">
<!-- Create headings. Every row is defined as with <tr> tag-->
<!-- Create headings. Every row is defined as with <tr> tag-->
<tr>
<th>Player ID</th>
<th>Player Name</th>
<th>Player Position</th>
<th>Delete</th>
</tr>
<?php
require "php/conn.php";
//Select query
$res = $db -> query ("SELECT * FROM player");
//Set pointer to 1
$res -> data_seek(0);
//Display data using while loop
while ($row = $res -> fetch_assoc()) {
// Write html code in php using this method
echo "<div class = 'row'>";
echo "<tr>";
echo "<td>".$row['id']."</td>";
echo "<td>".$row['surname']."</td>";
echo "<td>".$row['position']."</td>";
echo "<td><button id = 'dlt_btn' type='button'>Delete</button></td>";
echo "</tr>";
echo "</div>";
}
?>
</body>
?
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/style.css"></link>
<script src="js/jquery-1.11.3.min.js"></script>
<script src="js/script.js"></script>
</head>
<body>
<!-- Create a table-->
<table style width="800" border="1" cellpadding="1" cellspacing="1">
<!-- Create headings. Every row is defined as with <tr> tag-->
<!-- Create headings. Every row is defined as with <tr> tag-->
<tr>
<th>Player ID</th>
<th>Player Name</th>
<th>Player Position</th>
<th>Delete</th>
</tr>
<?php
require "php/conn.php";
//Select query
$res = $db -> query ("SELECT * FROM player");
//Set pointer to 1
$res -> data_seek(0);
//Display data using while loop
while ($row = $res -> fetch_assoc()) {
// Write html code in php using this method
echo "<div class = 'row'>";
echo "<tr>";
echo "<td>".$row['id']."</td>";
$player_id = $row['id'];
echo "<td>".$row['surname']."</td>";
echo "<td>".$row['position']."</td>";
echo "<td><button id = 'dlt_btn' value = ". $row['id'] . " type='button'>Delete</button></td>";
echo "</tr>";
echo "</div>";
}
?>
</body>
<?php
require "php/conn.php";
$player_id = $_POST['id'];
$stmt = $db -> prepare ("DELETE FROM player WHERE id = $player_id");
$stmt -> execute();
?>
delete.php
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/style.css"></link>
<script src="js/jquery-1.11.3.min.js"></script>
<script src="js/script.js"></script>
</head>
<body>
<!-- Create a table-->
<table style width="800" border="1" cellpadding="1" cellspacing="1">
<!-- Create headings. Every row is defined as with <tr> tag-->
<!-- Create headings. Every row is defined as with <tr> tag-->
<tr>
<th>Player ID</th>
<th>Player Name</th>
<th>Player Position</th>
<th>Delete</th>
</tr>
<?php
require "php/conn.php";
//Select query
$res = $db -> query ("SELECT * FROM player");
//Set pointer to 1
$res -> data_seek(0);
//Display data using while loop
while ($row = $res -> fetch_assoc()) {
// Write html code in php using this method
echo "<div class = 'row'>";
echo "<tr>";
echo "<td>".$row['id']."</td>";
echo "<td>".$row['surname']."</td>";
echo "<td>".$row['position']."</td>";
echo "<td><button id = 'dlt_btn' type='button'>Delete</button></td>";
echo "</tr>";
echo "</div>";
}
?>
</body>
?
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/style.css"></link>
<script src="js/jquery-1.11.3.min.js"></script>
<script src="js/script.js"></script>
</head>
<body>
<!-- Create a table-->
<table style width="800" border="1" cellpadding="1" cellspacing="1">
<!-- Create headings. Every row is defined as with <tr> tag-->
<!-- Create headings. Every row is defined as with <tr> tag-->
<tr>
<th>Player ID</th>
<th>Player Name</th>
<th>Player Position</th>
<th>Delete</th>
</tr>
<?php
require "php/conn.php";
//Select query
$res = $db -> query ("SELECT * FROM player");
//Set pointer to 1
$res -> data_seek(0);
//Display data using while loop
while ($row = $res -> fetch_assoc()) {
// Write html code in php using this method
echo "<div class = 'row'>";
echo "<tr>";
echo "<td>".$row['id']."</td>";
$player_id = $row['id'];
echo "<td>".$row['surname']."</td>";
echo "<td>".$row['position']."</td>";
echo "<td><button id = 'dlt_btn' value = ". $row['id'] . " type='button'>Delete</button></td>";
echo "</tr>";
echo "</div>";
}
?>
</body>
<?php
require "php/conn.php";
$player_id = $_POST['id'];
$stmt = $db -> prepare ("DELETE FROM player WHERE id = $player_id");
$stmt -> execute();
?>
您尝试过什么吗?有sql语句在运行吗?您知道如何进行ajax调用吗?您基本上需要一个ajax调用,该调用将该行的id发送到delete.php(加上身份验证,…)按钮不能多次具有相同的id。你可以在这里使用玩家的idid='dlt_btn.'$row['id']。“
我想他想从数据库中删除它,而不仅仅是从dom中删除。虽然这可以从HTML中删除它,但OP正在寻找外部调用以从数据库中删除某些内容。是的。我需要从数据库中删除,因为wellOP没有指定从数据库中删除。只提到jQuery。Post更新显示了包括ajax(无服务器端逻辑)的示例,尽管问题中没有直接指定,但在他的代码中,他确实建议“激活”PHP文件。你的答案改变了,我投了赞成票。