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 に答える