【问题标题】:Window confirm OK/Cancel strange behavior in Dialog Box?窗口确认确定/取消对话框中的奇怪行为?
【发布时间】:2014-02-11 17:00:48
【问题描述】:

(我之前发过一篇关于这个的帖子,但我会尽量在这个中更清楚一点。)

我在使用 windows.confirm 框时遇到了一些问题。我的点击事件会打开一个 jquery 对话框并加载一个对象列表。每个对象都有一个删除按钮,它删除并发送回一个更新的列表(通过 Django 视图)。

我正在尝试在删除之前进行确定/取消确认。它工作一次。我遇到的问题是下次我打开对话框时,单击删除我必须按两次确定/取消,然后按三下等等..

有什么想法吗? (我也试过 else return false)

$("#mylist").click(function(event) {
    event.preventDefault();
    $('#dialog').load($(this).attr('href')).dialog({
      width: 800,
      height: 530,
      resizable: false,
      title: "Dialog Title",
      autoOpen: true,
      modal: true
    });

$("#dialog").on("click", ".delete", function(event) {
  event.preventDefault();
  var val = window.confirm('Are you sure you want to delete this?');
  if(val == true) {
     $("#dialog").load($(this).attr("href"));
  } 
});
});

【问题讨论】:

  • 您确定没有多次附加click 事件吗?

标签: javascript jquery html


【解决方案1】:

在定义第二个函数之前尝试关闭第一个函数:

$("#mylist").click(function(event) {
    event.preventDefault();
    $('#dialog').load($(this).attr('href')).dialog({
      width: 800,
      height: 530,
      resizable: false,
      title: "Dialog Title",
      autoOpen: true,
      modal: true
    });
});

$("#dialog").on("click", ".delete", function(event) {
  event.preventDefault();
  var val = window.confirm('Are you sure you want to delete this?');
  if(val == true) {
     $("#dialog").load($(this).attr("href"));
  } 
});

【讨论】:

  • 好收获。如果不关闭第一个,则每次单击后都会重新绑定一次。
  • 是的,我认为这行得通!该死的,我真的是一个文盲......非常感谢!
【解决方案2】:

也许你的问题是包含确认的点击事件在#mtlist点击事件中。

试试这个:

$("#mylist").click(function(event) {
event.preventDefault();
   $('#dialog').load($(this).attr('href')).dialog({
   width: 800,
   height: 530,
   resizable: false,
   title: "Dialog Title",
   autoOpen: true,
   modal: true
  });
});

$("#dialog").on("click", ".delete", function(event) {
event.preventDefault();
var val = window.confirm('Are you sure you want to delete this?');
if(val == true) {
  $("#dialog").load($(this).attr("href"));
} 
});

【讨论】:

  • 是的,我想就是这样。谢谢!
【解决方案3】:

试试这个:

$("#mylist").click(function(event) {

    event.preventDefault();
    $('#dialog').dialog('destroy').remove();
    $('#dialog').load($(this).attr('href')).dialog({
      width: 800,
      height: 530,
      resizable: false,
      title: "Dialog Title",
      autoOpen: true,
      modal: true
    });

如果它破坏了一切,请尝试不删除

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-26
    • 1970-01-01
    相关资源
    最近更新 更多