我有一個文件可以根據數據庫中的記錄生成動態表。並在每一行旁邊放置一個按鈕來刪除該行。我的代碼現在運行良好,但只有第一行。從第2行開始,jquery不會檢測到被點擊的特定行。動態表中的jQuery表單不會檢測到第二行
這是我的PHP文件
while ($row = mysqli_fetch_array($queryresult)){
$id = $row['requestID'];
$customerID = $row['userID'];
$username = $row['username'];
$amount = $row['amount'];
$time = $row['requested_at'];
$status = $row['status'];
//display out the results to the user
echo "<tr>";
echo "<td> $username </td>";
echo "<td> $amount </td>";
echo "<td> $time </td>";
echo "<td> $status </td>";
echo "<td> <form method='POST'>
<input type='hidden' name='wdID' id='wdID' value='".$id."'/>
<input type='hidden' name='customerID' id='customerID' value='".$customerID."'/>
<input type='hidden' name='amount' id='amount' value='".$amount."'/>
<button class=\"btn btn-success btn-xs\" id=\"acceptWD\" title=\"Accept Withdraw Request\" type=\"button\"><i class=\"glyphicon glyphicon-ok\"></i></button>
</form>
</td>";
echo "</tr>";
}
mysqli_free_result($queryresult);
mysqli_close($connection); //close the database
,這是我的JavaScript文件
$("#acceptWD").click(function(event){
event.preventDefault();
var self = $(this);
var wdID = $(this).$("#wdID").val();
var customerID = $("#customerID").val();
var amount = $("#amount").val();
console.log(wdID);
$.ajax({
type: 'POST',
url: 'process_withdraw_request.php',
data: {wdID:wdID, customerID:customerID, amount:amount},
success: function(result){
//console.log(result);
if(result){
$.notify({
// options
message: 'The Request Has Been Accepted'
},{
// settings
delay: 2000,
allow_dismiss: false,
placement: {
from: "top",
align: "center"
},
type: 'success'
})
setTimeout(function(){
window.location.reload(1);
}, 4000);
}
}
});
});
我嘗試使用下面的代碼,但仍然沒有
$("#acceptWD").on('click', function(){
var a = $(this).closest('tr').val();
console.log(a);
});
請幫忙謝謝
'id'應該是唯一的 –
,每行的ID是不同的,先生,這已經是唯一正確的? – Alvin
'id = \「acceptWD \'',將它改爲class ...例如:'lass = \」btn btn-success btn-xs acceptWD \「',那麼選擇器將是'$('。acceptWD')' –