我正在尝试获取此td的ID ....当我检查我的网络控制台时,它说id = undefined。我不确定我做错了什么......
<td data-id="' . $fetch['userID'] . '" >
<button onClick = "deleterecord();">delete</button>
</td>
的javascript
function deleterecord() {
//var del_id = $(this).attr("id");
var del_id = $(this).closest('td').data('id');
var dataString = 'id=' + del_id;
if (confirm("Are you sure you want to delete this record?")) {
$.ajax({
type: "POST",
url: "deletemember.php",
data: dataString,
success: function () {
}
});
$(this).parents(".record").animate("fast").animate({
opacity: "hide"
}, "slow");
}
return false;
}
deletemember.php
$id = $_GET['id'];
$stmt = $conn->prepare("DELETE li.*
FROM `loginInfo` li
WHERE li.userID = ?");
$stmt->bind_param("s", $id);
$stmt->execute();
if ($stmt){
header('Location: memberinfo.php');
} else echo 'Delete failed. Try again';
答案 0 :(得分:1)
<强> HTML 强>
<td>
<button onClick = "deleterecord('123');">delete</button>
</td>
<强>的javascript 强>
function deleterecord(userID) {
if (confirm("Are you sure you want to delete this record?")) {
$.ajax({
type: "POST",
url: "deletemember.php",
data: {userID:userID},
success: function () {
}
});
$(this).parents(".record").animate("fast").animate({
opacity: "hide"
}, "slow");
}
return false;
<强> deletemember.php 强>
$id = $_POST['userID'];
$stmt = $conn->prepare("DELETE li.*
FROM `loginInfo` li
WHERE li.userID = ?");
$stmt->bind_param("s", $id);
$stmt->execute();
if ($stmt){
header('Location: memberinfo.php');
} else echo 'Delete failed. Try again';
答案 1 :(得分:1)
此代码将根据需要返回data-id
。请根据您的方便修改以下代码。
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.12.0.min.js">
</script>
<script>
$(document).ready(function(){
$('#delete_button').on('click', function(){
alert( $(this).closest('td').attr('data-id') );
});
});
</script>
</head>
<body>
<table>
<tr>
<td data-id="456" >
<button id="delete_button" >delete</button>
</td>
<tr>
</table>
</body>
</html>
答案 2 :(得分:0)
尝试修改HTML:
<td data-id="<?php echo $fetch['userID']?>" >
<button onClick = "deleterecord(<?php echo $fetch['userID']?>);">delete</button>
</td>
现在在JAvascript中:
function deleterecord(deleteid) {
if (confirm("Are you sure you want to delete this record?")) {
$.ajax({
type: "GET",
url: "deletemember.php",
data: {id:deleteid},
success: function () {
}
});
$(this).parents(".record").animate("fast").animate({
opacity: "hide"
}, "slow");
}
return false;