jQuery:在每次页面刷新时确认框循环

时间:2012-09-09 04:59:39

标签: javascript jquery

我循环遍历数据数组,每个数据都有自己的删除按钮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,你可以看到它正在做什么here

修改

毕竟这一次!!!而我所要做的只是

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

每页加载时不再增加确认框。 stopImmediatePropagation()是神奇的!

1 个答案:

答案 0 :(得分: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 ();
        }
    });
}