jQuery AJAX $ .post无法使用.click()方法

Nic*_*Law 0 javascript ajax jquery

我的ajax请求有点困难.似乎$ .post方法根本不起作用,没有发送请求.在firebug中也没有任何东西出现.

我可以让这个工作:

   $('.comment-remove').click(function (evt) {

        evt.preventDefault();
        var sure = confirm('Are you sure your want to remove your comment from this thread?');

        if(sure) {              
            var comment_id = $(this).attr('id');
            alert(member_id);
        }
    });
Run Code Online (Sandbox Code Playgroud)

但不是这个:

   $('.comment-remove').click(function (evt) {

        evt.preventDefault();
        var sure = confirm('Are you sure your want to remove your comment from this thread?');

        if(sure) {
            var comment_id = $(this).attr('id');

            $.post('comment.php', { comment_id: comment_id }, function(data) {
                if (data === 'success') {
                    alert(data);
                } else {
                    alert('Unable to remove comment at this time.');
                }
            }
        }
    });
Run Code Online (Sandbox Code Playgroud)

HTML是:

<a class="comment-remove" id="<?php echo $comment_id; ?>">Remove my comment</a>
Run Code Online (Sandbox Code Playgroud)

Nia*_*son 7

它应该是:

$.post('comment.php', { comment_id: comment_id }, function(data) {
  if (data === 'success') {
    alert(data);
  } else {
    alert('Unable to remove comment at this time.');
  }
})
Run Code Online (Sandbox Code Playgroud)

看到)我添加的额外内容?它在开始时匹配(in $.post(.你错过了它,它总是很容易做到.你的控制台应该可以告诉你这种事情.