【问题标题】:Alert with multiple options具有多个选项的警报
【发布时间】:2014-03-28 10:17:56
【问题描述】:

只是想知道,是否可以创建具有多个选项的警报?

例如,在 Facebook 中,当您在未完成输入消息的情况下尝试关闭选项卡/窗口时,会弹出带有“离开此页面”和“留在此页面”选项的警报。

【问题讨论】:

  • JavaScript alert with 3 buttons 的可能重复项
  • @Andy 但是你不能命名confirm()选项。
  • @Andy 那么 Facebook 网站的创建者是如何做到的呢?他是如何给警报提供两个名称除了“Ok”和“Cancel”的选项的?

标签: javascript alert


【解决方案1】:

表单示例,您正在寻找 window.onbeforeunload:

<script type="text/javascript">
var originalFormContent
var checkForChanges = true;

jQuery(document).ready(function () {
    originalFormContent = jQuery('#myForm input[type=text]').serialize() +      jQuery('#myForm select').serialize();
});

function onClose() {

    if (checkForChanges && originalFormContent != "undefined") {
        var content = jQuery('#myForm input[type=text]').serialize() + jQuery('#myForm select').serialize();
        if (content != originalFormContent) {

            return confirm('You have unsaved changes.  Click OK if you wish to continue ,click Cancel to return to your form.');
        }
    }

}

window.onbeforeunload = onClose(); 

【讨论】:

    【解决方案2】:

    你指的是window.onbeforeunload

    Best way to detect when a user leaves a web page?

    您还可以将window.confirm() 函数用于OK/Cancel 选项:

    http://jsfiddle.net/UFw4k/1

    除此之外,您还必须实现自定义模式警报,例如 jQuery Dialog

    【讨论】:

      【解决方案3】:

      请看http://jqueryui.com/dialog/#default

      从之前的回答中复制了这个:JavaScript alert with 3 buttons

      【讨论】:

      • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接的答案可能会失效。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-29
      • 1970-01-01
      相关资源
      最近更新 更多