javascript - jQuery AJAX $.post 不适用于 .click() 方法

标签 javascript jquery ajax

我的 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);
        }
    });

但不是这个:

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

HTML 是:

<a class="comment-remove" id="<?php echo $comment_id; ?>">Remove my comment</a>

最佳答案

应该是:

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

看到我添加的额外 ) 了吗?它与开头处的 $.post( 中的 () 相匹配。您错过了它,它总是很容易做到。您的控制台应该能够告诉您这种类型东西。

关于javascript - jQuery AJAX $.post 不适用于 .click() 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20204123/

相关文章:

javascript - 如何使用 VPN 测试 javascript 地理定位?

javascript - 在 Electron 中保存敏感数据

javascript - Jquery 更改 MVC 下拉列表中的选定值

javascript - 在没有文件的情况下从 HTML5 中的 javascript 播放音频 - 使用缓冲区

javascript - 与其他浏览器相比,为什么 IE9 的地理定位支持被破坏了?

javascript - 如何用 React 删除一个列表项(函数组件)

javascript - 子级不触发父级函数

javascript - 在计时器和 post ajax 请求之间传递对象(匿名函数)

javascript - Wordpress AJAX 不起作用 - 响应 0

javascript - 我应该如何单独发送表格?