Javascript jQuery:在每次页面刷新时确认框循环
我在一个数据数组中循环,每个数组都有自己的带有class=delete\u thereid的delete按钮。当页面加载时,删除按钮工作正常。。现在 在页面再次加载start_timer并尝试删除其他记录后,本机js确认框会弹出两次。。实际上,每次刷新页面时,弹出窗口都会增加。几天来,我一直在努力寻找解决方案,但都没有成功。这是我的密码Javascript jQuery:在每次页面刷新时确认框循环,javascript,jquery,Javascript,Jquery,我在一个数据数组中循环,每个数组都有自己的带有class=delete\u thereid的delete按钮。当页面加载时,删除按钮工作正常。。现在 在页面再次加载start_timer并尝试删除其他记录后,本机js确认框会弹出两次。。实际上,每次刷新页面时,弹出窗口都会增加。几天来,我一直在努力寻找解决方案,但都没有成功。这是我的密码 var refreshSpeed = 8000; var intValID = 0; function get_post () { $.aja
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 ();
}
});
每次页面加载时不再增加确认框。停止即时复制是神奇的 您可以尝试更改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 ();
}
});
}
那不管用。。但我确实在另一篇文章中找到了一些东西。。我正在测试它,如果它真的有效,我会发回一个答案。。我只是想等一下,让它再循环一些。非常感谢。另外,我仍然会使用这种方法,它更干净。在顶部编辑我的帖子,因为我还不能回答自己的问题。