简体   繁体   English

带有确认的jQuery UI对话框

[英]jQuery UI Dialog with Confirmation

I have a jQuery UI dialog that gets a line of text. 我有一个jQuery UI对话框,其中包含一行文本。 If this text is not contained in a localStorage dictionary, I insert it into the dictionary. 如果此文本未包含在localStorage词典中,则将其插入到词典中。 If it is present, I want to give the user the option not to overwrite the existing entry in the "ok" handler. 如果现在,我想给用户不要覆盖在“OK”处理现有条目的选项。

Because jQuery UI dialogs are stateful and persist across multiple calls unless explicitly removed (AFAICT), I'm not seeing a clear path to presenting the "are you sure you want to nuke your previous entry?" 因为jQuery UI对话框是有状态的,并且除非明确删除(AFAICT)才能在多个调用中保持不变,所以我看不到呈现“您确定要删除上一个条目吗?”的明确方法。 alert without resorting to ... uh ... alert. 警报而不求助于...呃...警报。

The question, succinctly stated: Can you create a confirmation box from inside a jQuery UI Dialog? 问题简单地说:您可以在jQuery UI对话框中创建确认框吗?

Thanks. 谢谢。

我没有使用jQuery UI对话框,但是您始终可以创建自己的html元素,并可以对它们进行任何操作,包括在jQuery对话框的顶部进行分层。

I guess you could have googled something to find these links: 我想您可以用Google搜索一些东西来找到这些链接:

Anyways have it and make fun: 无论如何都拥有它并取笑:

  1. JQuery Dialogs jQuery对话框
  2. Jquery Confirmation jQuery确认

Cheers!!! 干杯!!!

Ok, it turned out the best way I found to handle this was using closures. 好的,事实证明,我发现处理此问题的最佳方法是使用闭包。 Like this (pseudo-code): 像这样(伪代码):

getThingieName: handler(function() {
  var $dialog;
  $dialog = $('<div id="thingie-name-dialog" class="ui-widget"></div>').html("<p>Enter a name for this thingie</p>\n<input type=\"text\" id=\"dlg-thingie-name\" style=\"width: 80%\" />").dialog({
    autoOpen: false
  }, {
    title: 'enter a name',
    modal: true,
    buttons: {
      Add: function() {
        var value = $('#dlg-thingie-name').val();
        $(this).dialog('close');
        $('#thingie-name-dialog').remove();
        return handler(value);                  // <= closure to handle the onAdd
      },
      Cancel: function() {
        $(this).dialog('close');
        return $('#thingie-name-dialog').remove();
      }
    }
  });
  return $dialog.dialog('open');
}),

getConfirmation: function(message, handler) {
  var $dialog;
  $dialog = $('<div id="confirmation-dialog" class="ui-widget"></div>').html("<p>" + message + "</p>").dialog({
    autoOpen: false
  }, {
    title: 'confirm overwrite',
    modal: true,
    buttons: {
      Ok: function() {
        $(this).dialog('close');
        $('#confirmatio-dialog').remove();
        return handler(true);               // <= closure to handle onOk
      },
      Cancel: function() {
        $(this).dialog('close');
        $('#Thingie-name-dialog').remove();
        return handler(false);              // <= closure to handle onCancel
      }
    }
  });
  return $dialog.dialog('open');
}

// Calling sequence
Snippets.getSnippetName(function(value) {
  if (value == null) return false;
  if (localStorage.getItem(value)) {
    getConfirmation("This thingie, " + value + ", already exists. Overwrite?", function(response) {
      if (response) return localStorage.setItem(value, snippet);
    });
  } else {
    localStorage.setItem(value, snippet);
  }
}

This may not be the optimal code, but it does make the triggering of the dialogs dependent on the button push by embedding them in the handlers. 这可能不是最佳代码,但是通过将它们嵌入到处理程序中,确实使对话框的触发取决于按钮的按下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM