jQuery 暂停脚本执行的jQuery确认框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5383271/
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
jQuery confirm box that pauses script execution
提问by EvilAmarant7x
I'm looking for a jquery/nice looking alternative to the standard dialog box. jQUery UI has a nice one, but it doesn't pause script execution to wait for the response like confirm() does. The demo below is supposed to display two divs that display the choice of the preceding confirm box, but the jquery dialog box doesn't cause the script to wait.
我正在寻找一个 jquery/漂亮的替代标准对话框。jQUEry UI 有一个不错的 UI,但它不会像 confirm() 那样暂停脚本执行以等待响应。下面的演示应该显示两个div,显示前面确认框的选择,但jquery对话框不会导致脚本等待。
http://jsfiddle.net/EvilAmarant7x/r7Ur5/
http://jsfiddle.net/EvilAmarant7x/r7Ur5/
I specifically need something that causes the script to pause.
我特别需要一些导致脚本暂停的东西。
Thanks
谢谢
回答by mcgrailm
Your using the dialog in the wrong way
您以错误的方式使用对话框
you should do it like this
你应该这样做
$("#dialog-confirm").dialog({
resizable: false,
height: 140,
modal: true,
buttons: {
Proceed: function() {
call_true_function();
$(this).dialog("close");
},
Cancel: function() {
call_false_function()
$(this).dialog("close");
}
}
});
or something similar
或类似的东西
** EDIT **
** 编辑 **
so something like this ?
这样的事情?
count = 0;
$("#dialog-confirm").dialog({
resizable: false,
height: 140,
modal: true,
buttons: {
Proceed: function() {
proceed();
$(this).dialog("close");
},
Cancel: function() {
$(this).dialog("close");
}
}
});
function proceed(){
if (count != 10){
$("#dialog-confirm").dialog('open');
}else{
$('body').append("<div>Yes was selected " + count + " times!</div>");
}
}
回答by scurker
Unfortunately, this is not possible if you use a custom confirm/modal window. You're going to have to use callbacks or opt to use the new deferredobject introduced with 1.5.
不幸的是,如果您使用自定义确认/模式窗口,这是不可能的。您将不得不使用回调或选择使用1.5 引入的新延迟对象。
Using deferred would look something like this:
使用 deferred 看起来像这样:
var dfd = new $.Deferred();
$("#dialog-confirm").dialog({
resizable: false,
height: 140,
modal: true,
buttons: {
Proceed: function() {
dfd.resolve('Success!');
$(this).dialog("close");
},
Cancel: function() {
dfd.reject('Uh-oh!');
$(this).dialog("close");
}
}
});
dfd.then(function() {
// perform next action when user clicks proceed
});
dfd.fail(function() {
// perform some action when the user clicks cancel
});