我正在尝试使用确认方法,但由于某种原因,确认窗口弹出了几次。我用谷歌搜索并尝试了不同的东西,但不幸的是我无法让它正常运行。代码如下:
//user clicks on the delete button
$("#deletePopUpImage").click(function(){
console.log("deletePopUpimageCalled");
//get the id of the image
id = ($(this).parent().prop("id"));
//create the ajax request
data = "typ=function&functionType=deleteUserImage&id="+id;
//open the confirm box
var r = confirm("Are you sure that you want to delete this image?");
if (r == true) {
console.log("loadAjaxCAlled");
//Ajax call
loadAjax(data);
//hide the image and the loader
hideImagePopup();
} else {
//do nothing
}
});
奇怪的是,有时弹出确认窗口两次,有时弹出三次,有时像预期的那样弹出一次。这就是插入两个 console.log 的原因。
“deletePopUpimageCalled”总是只出现一次。但是“loadAjaxCAlled”出现了好几次。
在 Ajax 请求的成功回调中,我只是隐藏了缩略图 div。
你知道我上面的代码有什么问题吗?
谢谢 斯特凡
最佳答案
可能是附加事件的代码:
$("#deletePopUpImage").click(function(){...});
被多次调用。每次调用 .click(...) 都会生成一个新的处理程序,该处理程序会在单击按钮时触发。
一些浏览器将相同的条目叠加到一个条目中(因此日志不会扩展得那么快),这可能是您多次看不到“deletePopUpimageCalled”的原因。 最好通过在浏览器中调试来检查这一点。
关于javascript窗口确认弹出几次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33077221/