等待来自 Javascript 模式的“返回值”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11931906/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 07:44:14  来源:igfitidea点击:

Wait for "return value" from Javascript modal

javascriptjqueryhtmltwitter-bootstrapmodal-dialog

提问by Dr.Kameleon

OK, I'm by no means a JavaScript guru, and perhaps I'm still thinking in terms of Desktop software development, but this is what I'm trying to achieve :

好吧,我绝不是 JavaScript 大师,也许我还在考虑桌面软件开发,但这就是我想要实现的目标:

  • I'm using Twitter Bootstrap's Modals
  • In some cases, before an action takes place, I want to verify it with a "Are you sure?" (Yes/No) Modal Dialog.
  • 我正在使用Twitter Bootstrap 的 Modals
  • 在某些情况下,在采取行动之前,我想用“你确定吗?”来验证它。(是/否)模态对话框。

Question :

题 :

  • What should the inner logic be?
  • 内在逻辑应该是什么?

I mean :

我的意思是 :

  • User clicks buttonA (which ultimately performs actionA)
  • I want to launch the modal, wait for the input (either of the 2 buttons - yes/no) and/or pass it to some checking routine before performing(or not) actionA
  • 用户点击 buttonA(最终执行 actionA)
  • 我想启动模态,等待输入(2 个按钮之一 - 是/否)和/或在执行(或不执行)actionA 之前将其传递给某些检查例程

This my HTML code for the modal (should the buttons be - somehow - acting via their onclickjavascript routines?) - also note that #confirmMessageis going to be variable.

这是我的模态 HTML 代码(按钮应该 - 不知何故 - 通过他们的onclickjavascript 例程起作用?) - 还要注意这#confirmMessage将是可变的。

<div class="modal fade hide" id="confirmAlert">
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">x</button>
        <h3 id="confirmTitle">Are you sure?</h3>
    </div>

    <div class="modal-body">
        <p id="confirmMessage">Body</p>
    </div>
    <div class="modal-footer">
        <a href="#" class="btn" data-dismiss="modal">Cancel</a>
        <a href="#" class="btn btn-danger">Yes</a>
    </div>
</div>


Just an idea:

只是一个想法:

  • Write a function like checkBeforeExec(message,functionToExec)
  • Set #confirmMessageto message and Yes' href to javascript:functionToExec
  • Makes sense?
  • 写一个类似的函数 checkBeforeExec(message,functionToExec)
  • 设置#confirmMessage为 message 和 Yes' href 为javascript:functionToExec
  • 说得通?


I know it may sound a bit confusing - but I simply do not know what the most javascript-friendly way to do this would be...

我知道这听起来可能有点令人困惑 - 但我根本不知道最适合 JavaScript 的方法是什么......

采纳答案by moribvndvs

I created a dialog manager around modal, with confirmhelper function that does essentially this:

我围绕模态创建了一个对话框管理器,它的confirm辅助功能基本上是这样的:

var callback = function(result) {
  alert(result);
}); // define your callback somewhere

$('#confirmAlert').on('click', '.btn, .close', function() {
  $(this).addClass('modal-result'); // mark which button was clicked
}).on('hidden', function() {
  var result = $(this).find('.modal-result').filter('.btn-danger').length > 0; // attempt to filter by what you consider the "YES" button; if it was clicked, result should be true.

  callback(result); // invoke the callback with result
});

Also, you should make both the Cancel and Yes button data-dismiss="modal".

此外,您应该同时按下 Cancel 和 Yes 按钮data-dismiss="modal"

Here's a fiddle, with a simple example of my dialog manager.

这是一个小提琴,带有我的对话框管理器的简单示例

Note, that if you are using Bootstrap 3, you should add a handler to the hidden.bs.modalevent instead of hidden.

请注意,如果您使用的是Bootstrap 3,则应向hidden.bs.modal事件添加处理程序而不是hidden.

回答by Nick

With jquery you can use something like

使用 jquery,您可以使用类似的东西

$('.btn').click(function(){
   val=$(this).val()
   if(val=='Yes'){
     //continue the processing
   }
})