jQuery:确认框循环每次刷新页面 [英] jQuery: Confirm box loops on every page refresh

查看:77
本文介绍了jQuery:确认框循环每次刷新页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am looping through an array of data with each having there own delete button with class="delete_thereid". when the page loads the delete button works fine.. now after the page loads again (start_timer()) and I try to delete a different record, the native js confirmation box pops up two times.. actually the popup increments each time the page gets refreshed. I really have been at trying to find a solution for this for a few days now with out a success. Here is my code.

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,您可以在此处

the function that is making the call is deletePost from get_post, you can see what its doing here

编辑

所有这些时间之后!!!而我要做的就是

After all this time!!! and all I had to do was

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

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

No more incremented confirmation box on each page load. stopImmediatePropagation () is magical!

推荐答案

您可以尝试更改您的deletePost方法,

You could try changing your deletePost method,

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

Instead of iterating through a set of matching elements you can directly bind an event to them:

function deletePost () {
    $("[class^=delete]").on("click", function(e) {
        e.preventDefault ();
        //stop_timer ();
        if (confirmDelete()) {
            $(form).die ("click");
            $(form).unbind ().click();
            //e.stopPropagation();
            //start_timer ();
        }
    });
}

这篇关于jQuery:确认框循环每次刷新页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆