1

我正在遍历一组数据,每个数据都有自己的删除按钮class="delete_thereid"。当页面加载时删除按钮工作正常..现在页面再次加载后(start_timer())我尝试删除不同的记录,本机 js 确认框弹出两次..实际上每次页面刷新时弹出都会增加。几天来,我真的一直在努力寻找解决方案,但没有成功。这是我的代码。

var refreshSpeed = 8000;
var intValID = 0;

function get_post ()
{    
    $.ajax ({
         url:  "postComments.php",
         type: "post",
         cache: false,
         dataType: "json",
         data: {type: "getComments"},
         success: function (d) {
            var c = [];
            var r = [];
        v   ar cp = [];

            //Check for new post, update page.
            $(d.results.com).each (function (k, v) 
                        {
                if ($("#showComments").find ("div#"+$(v).attr ("id")).length == 0) {
                        cp.push (v);                   
                 }
                c.push ($(v).attr ("id"));

                if ($.inArray ($(v).attr ("id"), c_hashid) == -1) {
                    c_hashid.push ($(v).attr ("id"));
                }
            });

             $("#showComments").prepend (cp).fadeIn ("slow");

             remove_deleted (c_hashid, c); //remove post
             remove_deleted (r_hashid, r); //remove replies
             deletePost ();
             start_timer ();
             //optionBttn ();
             return false;
     }       
  });
}

function remove_deleted (ids, r)
{
      $.each (ids, function (k, v){

          if ($.inArray (v, r) == -1) 
          {
              $("#showComments").find ("div#"+v).slideUp ("slow");
          }
      }); 
}

function confirmDelete ()
{
    if (confirm ("Are you sure you wish to delete this post?")) {
        return true;
    }
    return false;
}


function deletePost ()
{
    $("[class^=delete]").each (function () {
        $(this).on("click", function (e) 
        {
            e.preventDefault ();
        //stop_timer ();
        if (confirmDelete ()) 
            {       
                $(this).die ("click");  //test
                $(this).unbind ().click(); //test

                //e.stopPropagation();
                //start_timer ();
        }

        });
    }); 
}

function start_timer () {
    intValID = setTimeout (function () {
                get_post ();
    }, refreshSpeed);
}

function stop_timer () {
    clearTimeout (intValID);
}

$(document).ready (function () 
{
    $("#cbutton").attr("disabled", "disabled");

    $(document).mouseup (function (e)
    {
        if ($("#layerOne").has (e.target).length === 0)
        {
            $("div[id^=replybox]").fadeOut ("fast");
        }
    });

    get_post ();
    textAreaAnim ();
    play_video ();
});

进行调用的函数是 get_post 中的 deletePost,你可以在这里看到它在做什么

编辑

过了这么久!!!我所要做的就是

$("[class^=delete]").on ("click", function (e) 
{
    if (confirmDelete ())
    {
        e.stopImmediatePropagation () //<----this!!! this is all I needed
        //dorestofstuff ();
    }
});

每个页面加载时不再增加确认框。stopImmediatePropagation() 很神奇!

4

1 回答 1

0

你可以试试换个deletePost方法

您可以直接将事件绑定到它们,而不是遍历一组匹配的元素:

function deletePost () {
    $("[class^=delete]").on("click", function(e) {
        e.preventDefault ();
        //stop_timer ();
        if (confirmDelete()) {
            $(form).die ("click");
            $(form).unbind ().click();
            //e.stopPropagation();
            //start_timer ();
        }
    });
}
于 2012-09-09T05:24:30.000 回答