0

我在这里用头撞墙,希望我对明显的事情视而不见。

我有这个 DOM 结构:

<ul>
    <li>
        <h3>Lorem ipusm</h3>
        <p class="remove-story"><a href="http://example.com/remove/XX">Delete</a></p>
    </li>
</ul>

这是我的 jQuery:

$(".remove-story a").click(function()
    {
        var parent = $(this).closest('li');
        $.get($(this).attr('href'), function()
        {
            $(parent).fadeOut();
        });
        return false;
    });

就目前而言,单击其中的链接.remove-story没有任何作用,链接中的 URL 触发的操作也不会发生。

在页面加载或单击链接时,控制台中不会弹出 JS 错误。

如果我删除该$.get功能并简单地淡出列表项,则可以按预期工作。

如果我手动访问 URL(或删除return false并单击链接),则链接有效并且后端操作完成(故事已删除)。

我的代码中是否有任何人都可以发现的错误?如果没有,关于在哪里寻找故障排除的任何想法?

4

3 回答 3

1

将您的 onclick 包装为$(document).ready(function() {})

$(".remove-story > a").click(function(e)
    {
        var parent = $(this).closest('li');
        alert($(this).attr('href'));
        $.ajax({
            url: $(this).attr('href'),
            success: function() {
                alert(12);
                $(parent).fadeOut();
            },
            error: function(e) {
                alert('baaaahhhh:' + e);
            }
        })
         //e.stopPropagation();
        return false;
    }); 


​

更新:更改$.get$.ajax查看执行 Ajax 请求时是否出现错误。

于 2012-12-17T16:14:32.700 回答
1

I would firstly check if that get request really been send our, I mean using some tool like Fiddler or Chrome developer tool network tab, and then maybe add break point inside the call see if the parent you in that certain context is nothing or a wrong object

于 2012-12-17T16:18:08.223 回答
0

您需要对链接进行编码encodeURIComponent($(this).attr('href'));

这是一个 jsfiddle 工作:http: //jsfiddle.net/9Wnt8/

于 2012-12-17T17:04:28.570 回答