2

我有很多行

  1. 列表项 - 删除
  2. 列表项 - 删除
  3. 列表项 - 删除

像上面的 3 行.. 它有删除链接,点击时我想禁用该链接,直到我得到 ajax 的回复说可以删除它或不可以,不要删除它,用户不是所有者,然后我必须给元素回到可点击..如果这有意义

基本上防止点击元素,然后从后端恢复其元素错误。

我需要使用什么方法?我使用live方法,如果我使用die,那我将如何再次恢复?

4

2 回答 2

3

根据要求,这是一个使用链接而不是按钮的版本。

<ol>
    <li id="item-1">List item <a href="delete.php?1">Delete</a></li>
    <li id="item-2">List item <a href="delete.php?2">Delete</a></li>
    <li id="item-3">List item <a href="delete.php?3">Delete</a></li>
</ol>

JS对此的支持将是

$("ol").click(function(evt){
    if (evt.target.nodeName != "A") {
        return true;// it's not the delete link, so we don't have to do anything
    }
    var listItem = $(evt.target.parentNode);
    if (listItem.hasClass("disabled")) {
        return true;// we're still processing this already deleted list item
    }
    listItem.addClass("disabled");// lock the list item (perhaps visually too)

    // set up the AJAX call
    $.post("evaluator.php", {listId : listItem.attr("id")}, function(data){
        // let's suppose that we get "1" if the user can perform the procedure
        if (data == "1") {
           // request approved, remove the list item
           listItem.fadeOut("fast", function(){
               $(this).remove();
           });
        } else {
            // release the lock
            listItem.removeClass("disabled");
            // request denied, rollback
            alert("Sorry, you can't do that.");
        }
    });
    // here we stop the click event, so the URL in href won't be called.
    // you can "disable" a link by returning false on a click event
    // e.g. <a href="url" onclick="return false">disabled link</a>
    return false;
});
于 2009-10-08T11:04:26.590 回答
1

最好的方法是创建一个单独的函数来处理 AJAX 调用,所以你可以这样做

$(".delete").bind("click", deleteItem);

在你的 deleteItem 函数中,你可以通过这样做来解除绑定

function deleteItem(delBtn) {
    $(delBtn).unbind("click", deleteItem);
    //ajax call
    onerror: function() {
        $(delBtn).bind("click", deleteItem);
    }
}

编辑:意识到您正在使用live,这只是bind. 因此,在上面的示例中,您只需切换bind关键字 forliveunbindfor die。它应该做同样的事情(:

于 2009-10-08T09:06:19.580 回答