<script>
$(document).ready(function(){
$(".delete_buttom").click(function(){
var x = $(this).attr('id');
click_delete(x);
});
function click_delete(commentId){
$.post("ajax_comments3.php",
{
task : "this is the task",
commentId : commentId
}).success(
function(data){
$('#comment_' + commentId).remove();
}).error(function(){
alert("404 not found");
});
}
</script>
=============================================== =======================
<?php
if(isset($_POST['task']) && $_POST['task'] == "this is the task"){
$server_id = $_POST['ajax_id'];
$server_comment = $_POST['ajax_text'];
$user_name = $_POST['ajax_uname'];
$connection = mysqli_connect("localhost","root","","comments")
$insert_query = "insert into comment (commet_text,user_id)
values ('$server_comment','$server_id')";
$excute_insert = mysqli_query($connection,$insert_query) or die("insert query error");
?>
<li class="li_style" id="comment_<?php echo mysqli_insert_id($connection) ?>">
<img src="profile.jpg" class="user_img_src" />
<h5 class="username"><?php echo $user_name ; ?></h5>
<div class="delete_buttom" id="comment_<?php echo mysqli_insert_id($connection) ?>">X</div>
<div class="user_comment"><?php echo $server_comment ; ?> </div>
</li>
<?php
}
?>
当我输入一个新的评论它插入数据库并出现在网络浏览器中但问题是我有一个删除按钮,当我点击它后立即键入它不起作用我必须重新加载页面删除按钮适用于任何解决方案。
答案 0 :(得分:0)
正确。那是因为新按钮上没有任何事件。
也许是这样的:
var deleteButton = function() {
$(".delete_buttom").off('click');
$(".delete_buttom").on('click', function() {
var x = $(this).attr('id');
click_delete(x);
});
}
现在,在您的成功(添加新条目之后):
success(function(data) {
// add your entry...
deleteButton();
});