簡體   English   中英

當我添加新評論時,刪除按鈕在重新加載頁面后才起作用?

[英]when i add new comment the delete button doesn't work till i reload the page?

第一頁

ajax和jquery代碼

 <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");  

?>  

這是將插入到Ultage中的生命(我在HTML代碼中有Ultage)

    <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   

    }

?>  

當我輸入新評論時,它也會插入數據庫並出現在Web瀏覽器中,但問題是我在輸入評論后立即單擊它時有一個刪除按鈕,它不起作用,我必須重新加載頁面刪除按鈕適用於任何解決方案。

正確。 這是因為該新按鈕上沒有事件。

也許是這樣的:

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();
 });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM