我需要在ajax更新後更新具有唯一標識的td的內容。如何在ajax更新後更改td內容
但是我的方法不工作,ajax成功,但td只會在頁面重新加載後更新。
$(document).ready(function(){
$('body').on('click', '.editButton', function(e){
var postID = $(this).val();
var postURL = "../Controller/update.php";
var postNama = $("#nama"+postID).val();
var postMSISDN = $("#msisdn"+postID).val();
var postSMS = $("#sms"+postID).val();
$.ajax({
type: "POST",
url: postURL,
data:
{
nama: postNama,
msisdn: postMSISDN,
sms: postSMS,
id: postID
},
success: function(data){
$("#nama"+postID).html(postNama);
$("#msisdn"+postID).html(postMSISDN);
$("#sms"+postID).html(postSMS);
}
});
e.preventDefault();
});
});
的HTML我附和道:
<tr id='tr".$data['id']."'>
<td>".$data['id']."</td>
<td id='nama".$data['id']."'>".$data['nama']."</td>
<td id='msisdn".$data['id']."'>".$data['msisdn']."</td>
<td id='sms".$data['id']."'>".$data['sms']."</td>
<td><button type='button' class='btn btn-default' data-toggle='modal' data-target='#editModal".$data['id']."'>Edit</button></td>
<td><button type='button' class='btn btn-danger' data-toggle='modal' data-target='#deleteModal".$data['id']."'>Delete</button></td>
</tr>
update.php:
<?php
require_once "../Model/Model.php";
$nama = $_POST["nama"];
$msisdn = $_POST["msisdn"];
$sms = $_POST["sms"];
$id = $_POST["id"];
updateData($nama, $msisdn, $sms, $id);
?>
model.php:
function updateData($nama, $msisdn, $sms, $id){
try{
$conn = new PDO('mysql:host=localhost; dbname=crud', 'root', '');
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e){
echo 'ERROR: ' . $e->getMessage();
}
$conn->prepare("UPDATE data SET nama = ?, msisdn = ?, sms = ? WHERE id = ?")->execute([$nama, $msisdn, $sms, $id]);
}
你能顯示html嗎? –
我編輯了我的帖子@ShaktiPhartiyal –
你是否將從服務器返回的數據應用到你的ID上?如果是的話,是從服務器返回一個JSON數據? –