【问题标题】:Attach a modal window to a button and wait for the modal window to close before continue with the button action将模态窗口附加到按钮并等待模态窗口关闭,然后再继续按钮操作
【发布时间】:2018-02-16 15:36:31
【问题描述】:

我已将模式窗口附加到按钮。我想在单击按钮时打开模式窗口并显示一条消息。当模态窗口关闭时,继续按钮操作(打开一个新框架)。目前我看到模态窗口几秒钟,然后单击按钮打开一个新框架,我的模态消失了。

这是我的代码: 我的模态窗口 css + html

    <style>
/* The Modal (background) */
.modal {
    display: visible; /* Hidden by default */
    position: fixed; /* Stay in place */
    z-index: 1; /* Sit on top */
    left: 0;
    top: 0;
    width: 100%; /* Full width */
    height: 100%; /* Full height */
    overflow: auto; /* Enable scroll if needed */
    background-color: rgb(0,0,0); /* Fallback color */
    background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
}

/* Modal Content/Box */
.modal-content {
    background-color: #fefefe;
    margin: 15% auto; /* 15% from the top and centered */
    padding: 20px;
    border: 1px solid #888;
    width: 80%; /* Could be more or less, depending on screen size */
}

/* The Close Button */
.close {
    color: #aaa;
    float: right;
    font-size: 28px;
    font-weight: bold;
}

.close:hover,
.close:focus {
    color: black;
    text-decoration: none;
    cursor: pointer;
}


</style>


<div id="myModal" class="modal">

  <!-- Modal content -->
  <div class="modal-content">
    <span class="close">&times;</span>
    <p>Some text in the Modal..</p>
  </div>

</div>

我的 JS 伪代码:

.....
let popUpInstruction = require("!text-loader!./Instructions.html");
let editButton = frame.contents().find('input[name$="edit"]'); // just for testing bound it to the Edit button
....
if ( someStatement) {

let place = frame.contents().find(".headerContent");
let popUpInstructionHTML = $($.parseHTML(popUpInstruction));
popUpInstructionHTML.appendTo(place);
let closeButton = popUpInstructionHTML.contents().find(".close")[0];

               editButton.on("click", function () { // just for testing bound it to the Edit button
                        popUpInstructionHTML.show();

//how to make the code to stop here and wait for popUpInstructionHTML to be closed , brefore continue with the editButton action ?


                        // When the user clicks on <span> (x), close the modal
                        $(closeButton).on("click", function() {
                            popUpInstructionHTML.hide();
                        });
                });
}

这是我的按钮:

<input value=" Edit " class="btn" name="edit" title="Edit" type="button" onclick="navigateToUrl('url','DETAIL','edit');">

单击编辑按钮会加载另一个框架并关闭我的模态窗口。

【问题讨论】:

    标签: javascript typescript simplemodal


    【解决方案1】:

    您可以使用 jQuery 来trigger custom events。所以这样的事情应该可以工作:

    let closeButton = popUpInstructionHTML.contents().find(".close").first();
    
    editButton.on("click", function () { // just for testing bound it to the Edit button
        popUpInstructionHTML.show();
    
        // at some point you are going to call:
        // popUpInstructionHTML.trigger("finished");
    });
    
    popUpInstructionHTML.on("finished", function(){
        // When the user clicks on <span> (x), close the modal
        $(closeButton).on("click", function() {
            popUpInstructionHTML.hide();
        });
    })
    

    我不太明白为什么要在附加 closeButton 点击​​处理程序之前等待弹出窗口关闭......但希望你能从这个答案中提取你需要的内容并修改它以适应你的情况。

    【讨论】:

    • 感谢您的回答,不幸的是它不起作用。这个想法是使用模态窗口,例如 alert() 函数的工作原理。警报功能将停止并等待用户单击“确定”,然后它将继续。我想将其与模态窗口一起使用。主要问题是我正在操作一个按钮(我无权访问代码及其功能)。
    • 是的,但您没有使用alert()input()。这两个功能都是由浏览器实现的,可以在等待用户时阻止所有页面交互。您无法通过自定义弹出窗口来实现这一点,因此您必须将功能分解并分两部分实现。
    • 谢谢,我已经尝试过您的建议,但无法实现。我也尝试过 preventDefault() 并返回 false,但按钮继续执行其 onclick 操作...
    • 老实说,我并不完全理解你的设计,所以我的建议很可能是有缺陷的......我只是接受了你的“stop here”评论。您需要将其视为一系列独立的步骤,而不是单个代码流。你不能在 JavaScript 中完全“停在这里”......但你可以做一些事情,比如“让这个挂起,我会回来响应另一个事件”等等。
    • 我正在为网站引擎开发一个插件项目,类似于 Facebook。让我们以 Facebook 为例。我想创建一个 Chrome 插件,安装后它会在我单击时产生一个模式窗口,比如说 Messenger 按钮。简而言之,我安装插件 -> 点击 Messenger 按钮 -> Messenger 加载暂停 -> 出现模态窗口 -> 我关闭模态窗口 -> 恢复信使加载。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-06
    • 2011-06-29
    • 1970-01-01
    相关资源
    最近更新 更多