Javascript 删除表中每一行的按钮
我有一段javascript代码:Javascript 删除表中每一行的按钮,javascript,php,jquery,html-table,Javascript,Php,Jquery,Html Table,我有一段javascript代码: <td style="text-align: left;"> <?php echo '<button type="button" class="btn btn-danger delete delete-account"><i class="fa fa-trash-o fa-lg"></i> Delete</a></button>
<td style="text-align: left;">
<?php
echo '<button type="button" class="btn btn-danger delete delete-account"><i class="fa fa-trash-o fa-lg"></i> Delete</a></button>
<input type="hidden" value="'. $r['user_id'] .'" name="delete[]">';
?>
</td>
$('button.delete account')。单击(函数(e){
游泳({
标题:“你确定吗?”,
text:“您将无法撤消此帐户!”,
键入:“警告”,
showCancelButton:true,
confirmButtonColor:“#DD6B55”,
confirmButtonText:“是的,删除它!”,
CloseOnConfig:false,
html:false
},函数(){
swal(“已删除!”,“您想象的文件已被删除。”,“成功”);
});
});
$r['user\u id']-->这基本上是一个帮助我从数据库中提取所有用户的函数,例如,它将提取所有的用户id
问题是我在每一行都有不同的用户,当我点击删除按钮时,我得到的是相同的用户id,而不是个人id
我怎样才能单独呼叫每个用户
谢谢大家:)这两个脚本就可以了 show.php
$('button.delete-account').click(function(e) {
swal({
title: "Are you sure?",
text: " <?php echo $r['user_id'] ?> You will not be able to undo this acction !",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "Yes, delete it!",
closeOnConfirm: false,
html: false
}, function() {
swal("Deleted!", "Your imaginary file has been deleted.", "success");
});
});
delete.php
<?php
/*
Deletes a specific entry from the table
*/
// connect to the database
include('connect-db.php');
// check if the 'id' variable is set in URL, and check that it is valid
if (isset($_GET['id']) && is_numeric($_GET['id']))
{
// get id value
$id = $_GET['id'];
// delete the entry
$result = mysql_query("DELETE FROM *fillin* WHERE id=$id")
or die(mysql_error());
// redirect back to the view page
header("Location: urlocation.php");
}
else
// if id isn't set, or isn't valid, redirect back to view page
{
header("Location: urlocation.php");
}
?>
这两个脚本就可以了
show.php
$('button.delete-account').click(function(e) {
swal({
title: "Are you sure?",
text: " <?php echo $r['user_id'] ?> You will not be able to undo this acction !",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "Yes, delete it!",
closeOnConfirm: false,
html: false
}, function() {
swal("Deleted!", "Your imaginary file has been deleted.", "success");
});
});
delete.php
<?php
/*
Deletes a specific entry from the table
*/
// connect to the database
include('connect-db.php');
// check if the 'id' variable is set in URL, and check that it is valid
if (isset($_GET['id']) && is_numeric($_GET['id']))
{
// get id value
$id = $_GET['id'];
// delete the entry
$result = mysql_query("DELETE FROM *fillin* WHERE id=$id")
or die(mysql_error());
// redirect back to the view page
header("Location: urlocation.php");
}
else
// if id isn't set, or isn't valid, redirect back to view page
{
header("Location: urlocation.php");
}
?>
更新:无法完全理解您的代码,但您可以通过查看此提琴了解我的想法:
使用已创建按钮的值设置用户id是一种方法。这样,您将为每个创建的按钮分配一个值,该值表示用户的id
<?php
*/ Displays all data from ' table
// connect to the database
include('dbconnectfile.php');
// get results from database
$result = mysql_query("SELECT * FROM dbase")
or die(mysql_error());
// display data in table
echo "<p><b>info</b></p>";
echo "<table border='2' border-color='#800000' cellpadding='10'>";
echo "<tr> <th>fill-in</th> <th>fill-in</th><th>fill-in</th><th>Delete</th></tr>";
// loop through results of database query, displaying them in the table
while($row = mysql_fetch_array( $result )) {
// echo out the contents of each row into a table
echo "<tr>";
echo '<td>' . $row['rowname'] . '</td>';
echo '<td>' . $row['rowname'] . '</td>';
echo '<td>' . $row['rowname'] . '</td>';
echo '<td><a HREF="delete.php?id=' . $row['id'] . '"><img style="width: 25px; height: 25px; " alt="" src="">Delete</a></td>';
echo "</tr>";
}
// close table>
?>
未测试代码。。。我不太能够验证你的代码是如何工作的,所以我只是复制了你的代码,并用按钮的值做了一些小的更改。。但最重要的是,您要了解在每个按钮上存储用户的值(id),并在处理过程中使用$(此)检索该值。更新:无法完全理解您的代码,但您可以通过查看此小提琴了解我的想法:
使用已创建按钮的值设置用户id是一种方法。这样,您将为每个创建的按钮分配一个值,该值表示用户的id
<?php
*/ Displays all data from ' table
// connect to the database
include('dbconnectfile.php');
// get results from database
$result = mysql_query("SELECT * FROM dbase")
or die(mysql_error());
// display data in table
echo "<p><b>info</b></p>";
echo "<table border='2' border-color='#800000' cellpadding='10'>";
echo "<tr> <th>fill-in</th> <th>fill-in</th><th>fill-in</th><th>Delete</th></tr>";
// loop through results of database query, displaying them in the table
while($row = mysql_fetch_array( $result )) {
// echo out the contents of each row into a table
echo "<tr>";
echo '<td>' . $row['rowname'] . '</td>';
echo '<td>' . $row['rowname'] . '</td>';
echo '<td>' . $row['rowname'] . '</td>';
echo '<td><a HREF="delete.php?id=' . $row['id'] . '"><img style="width: 25px; height: 25px; " alt="" src="">Delete</a></td>';
echo "</tr>";
}
// close table>
?>
未测试代码。。。我不太能够验证你的代码是如何工作的,所以我只是复制了你的代码,并用按钮的值做了一些小的更改。。但最重要的是,您需要在每个按钮上存储用户的值(id),并在处理过程中使用$(这)检索它。您也可以从输入中获取id(我假定您使用的是jquery)
未测试您也可以从输入中获取id(我假设您使用的是jquery)
未在html上测试:
var id = $(e).parent().find('input[type="hidden"]').val();
在delete.php上:
$('button.delete-account').click(function(e) {
var id = $(this).siblings('input.userId').val();
swal({
title: "Are you sure?",
text: id + " You will not be able to undo this acction !",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "Yes, delete it!",
closeOnConfirm: false,
html: false
}, function(){
$.post( "delete.php", { userID: id } );
swal("Deleted!",
"Your imaginary file has been deleted.",
"success");
});
});
关于html:
var id = $(e).parent().find('input[type="hidden"]').val();
在delete.php上:
$('button.delete-account').click(function(e) {
var id = $(this).siblings('input.userId').val();
swal({
title: "Are you sure?",
text: id + " You will not be able to undo this acction !",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "Yes, delete it!",
closeOnConfirm: false,
html: false
}, function(){
$.post( "delete.php", { userID: id } );
swal("Deleted!",
"Your imaginary file has been deleted.",
"success");
});
});
为什么每行都有一个以上的用户?哦,对不起,我不是说每行都有一个用户,而是说每行都有一个用户。我更新了我的问题,对不起,你能给我们看看你的表格吗?屏幕截图或htmlwhy未设置删除按钮上的值。。这样,您就可以通过获取jquery中按钮的值来知道要删除哪个用户。为什么每行有多个用户?哦,对不起,我不是说每行有一个用户,而是说每行有一个用户。我已经更新了我的问题,对不起,您能给我们看看您的表吗?屏幕截图或htmlwhy未设置删除按钮上的值。。这样,您就可以通过在jquery中获取按钮的值来知道要删除哪个用户