<script type="text/javascript" src="../js/jquery-1.4.1.min.js"></script>
<script type="text/javascript">
$("#submit").click(function (){
var name =$("#name").val();
var mobile =$("#mobile").val();
var email =$("#email").val();
$.get('new.php',{name : name, mobile: mobile, email: email},function (data){
$("#res").html(data);
});
$("#name").val("");
$("#mobile").val("");
$("#email").val("");
$("#res").hide(10000);
});//submit.click end here!!!!!!!
$("#view").click(function(){
var view='view';
$.post('new.php',{view: view},function(data){
$("#viewdata").html(data);
});
$("#viewdata").toggle("slow");
});//to display data in table!!!!!!!
$("#add").click(function(){
$("#addnew").toggle("slow");
});//toggel button for record table!!!!!
$("body").on('click','',function(){
var iddata =this.id.split('-');
var id = iddata[1];
alert(id);
});
</script>
if(isset($_POST['view']))
{
$query="select * from userdetail";
$res=mysql_query($query);
echo "<table border=1>
<tr><th>name</th><th>Mobile</th><th>Email</th><th>Delete</th><th>Update</th></tr>";
while($row=mysql_fetch_array($res))
{
echo "<tr><td>".$row[1]."</td><td>".$row[3]."</td><td>".$row[4]."</td>";
echo "<td><div class='del' id='id-".$row[0]."';>Delete</div></td>";
echo "<td><a href='#'>Update</a></td>";
}
echo"</table>";
}
<body>
<div id="addnew" style="width:500px; float:left;">
<div style="width:200px; float:left;">
<form name="frm" id="frm">
<input type="text" id="name"></br>
<input type="text" id="mobile"></br>
<input type="text" id="email"></br>
<input type="button" id="submit" value="submit"/></br>
</form>
</div>
<div id="res" style="width:300px; float:left;">
</div>
</div>
<button id="add">Add New</button>
<button id="view">View</button>
<div id="viewdata">
<table border=1>
<tr>
<th>name</th><th>Mobile</th><th>Email</th><th>Delete</th><th>Update</th>
</tr>
</div>
</body>
我是 jquery 的新手,正在尝试使用 jquery 和 php 学习 crud 函数。我正在编写一个代码,我可以通过 jquery 添加数据,也可以从数据库中检索它,现在我已经放置了一个删除字段来从该表中删除记录,但我被困在这里,因为我不知道如何从该表中获取 id,以便我可以使用它从表中删除该记录。我已经尝试在 jquery 中使用 .on 和 delegate 来获取该 id 但仍然没有得到我想要的结果,所以请帮助我解决这个错误。
最佳答案
所以对于删除单元格,您的 html 输出将是这样的
<td>
<div class='del' id='id-1'>Delete</div>
</td>
在 JQuery 中你可以这样做:
$('.del').on('click',function() {alert(this.id)});
结果: id-1
关于php - 我不知道如何在使用 jquery 删除记录时获取记录的 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32780137/