-2

我正在创建一个运行良好的评论系统,但删除按钮不执行任何操作我正在使用 php mysql jquery ajax 我花了过去 3 个小时试图弄清楚如何修复此错误并使其正常工作,但这并没有发生在我身上

所以任何人都可以帮助我

评论框.php

 <div class="comment-buttons-holder">
            <ul>
                <li id="<?php echo $comment->comment_id; ?>" class="delete-btn">X</li>
             </ul>
          </div>

comment_delete.js (这是我正在学习的地方,这个测试没有显示任何内容)

$(document).ready(function() {
     $('.delete-btn').each(function() {
        var btn = this;
        $(btn).click(function(){
           console.log( "this id: " + btn.id );
        });
    }); 
});

评论删除.php

    <?php
     if(isset($_POST['task']) && $_POST['task'] == 'comment_delete')
      {
        require_once('../includes/db_connect.php'); 

        //echo "i am in the server side and i heard that you want delete";

         require_once('../models/comments.php');


          if(class_exists('comments'))
          {
              if(Comments::delete($_POST['comment_id']))
              {
                  echo "true";
              }

          }
          echo "false";

      }
    ?>

这是评论类(包含删除功能)

<?php
require_once('subscribers.php'); 


class Comments
{
     public static function getComments()
     {
         $output = array();
         $sql = "select * from comments order  by comment_id desc";
         $query = mysql_query($sql);

         if($query)
         {
             if(mysql_num_rows($query) > 0)
             {
                 while($row = mysql_fetch_object($query))
                 {
                     $output[] = $row;
                 }
             }
         }   
         return $output;
     }
     // return stdClass object  from the database
     public static function insert($comment, $userId)
     {
         //  insert data into database 
         $comment = addslashes($comment);
         $sql = "insert into comments values('', '$comment', '$userId')";
         $query = mysql_query($sql);

         if($query)
         {
             $insert_id = mysql_insert_id();

             $std = new stdClass();
             $std->comment_id = $insert_id;
             $std->comment = $comment;
             $std->userId =(int)$userId;

              return $std;
         }
         return null;

     }
     public static function update($data)
     {

     }
     public static function delete($commentId)
     {

         $sql = "delete from comments where comment_id=$commentId";
         $query = mysql_query($sql);
         if($query)
         {
             return true;
         }
         return null;
     }


}



?>
4

2 回答 2

0

试试这个

$(document).ready(function() {
     $('.delete-btn').click(function(){
           console.log( "this id: " + $(this).attr('id') );
        });
    }); 
});
于 2013-04-16T17:36:13.317 回答
0

以下是如何对bindjquery 对象集上的事件执行函数:

$(document).ready(function() {
    $('.delete-btn').bind('click', function() {
         console.log( "this id: " + btn.id );
    });
});

处于文档就绪状态。

于 2013-04-16T17:36:37.617 回答