function ValidateField(){
var bAllow= true;
//some checking here
if (bAllow == true && apl.val().trim() == "")
{
showDialog();
showDialog().done(function() {
return true; // wanna return true, but not success
}).fail(function() {
return false; //wanna return false, but not success
});
return false; //stop it to execute to next line
}
return bAllow; //success return }
function showDialog(){
var def = $.Deferred();
var modPop = '<div id="diaCom" title="Information?"><p>something something</p></div>';
$("#diaCom").remove();
$(modPop).appendTo('body');
$("#diaCom").dialog({
resizable: false,
draggable: false,
height:150,
width:300,
modal: true,
buttons: {
"Ok": function() {
def.resolve();
$(this).dialog("close");
},
"Cancel": function() {
def.reject();
$(this).dialog("close");
}
}
});
return def.promise();
}
//on click
if (validateField() == true){
//do something
}else{
//do something
}
大家好,有机会返还值(value)吗?我希望通过 showDialog().done() 返回 true 和 false,并为 validatefield() 函数返回失败,但它没有按我想要的方式工作,我无法分配给 bAllow,因为我已经有一个 return false 来保持执行下一行的对话框,知道吗?或者这样做是正确的?
最佳答案
好吧,这可行。
你的对话框函数... showDialog()
function confirmation(question) {
var defer = $.Deferred();
$('<div></div>')
.html(question)
.dialog({
autoOpen: true,
modal: true,
title: 'Confirmation',
buttons: {
"Yes": function () {
defer.resolve(true); //answer
$(this).dialog("close");
},
"No": function () {
defer.resolve(false); //answer
$(this).dialog("close");
}
},
close: function () {
$(this).remove(); //removes this dialog div from DOM
}
});
return defer.promise();
}
然后是使用函数的代码...
function onclick(){
var question = "Do you want to start a war?";
confirmation(question).then(function (answer) {
if (answer) {
alert("this is obviously " + answer);//TRUE
} else {
alert("and then there is " + answer);//FALSE
}
});
}
这对大多数人来说似乎是错误的。但总有一些情况是您不能不从 JQuery 对话框返回。
这将基本上模仿 confirm() 函数。但是有丑陋的代码和漂亮的确认框外观:)
我希望这能帮助一些人。
**
编辑:Bootstrap 3 解决方案
**我现在正在使用 [NakuPanda 的][1] Bootstrap 库,它工作得非常好。与 JQueryUI 基本相同,但在 Bootstrap UI 中。
function bsConfirm(question) {
var defer = $.Deferred();
BootstrapDialog.show({
type: BootstrapDialog.TYPE_PRIMARY,
title: 'Confirmation',
message: question,
closeByBackdrop: false,
closeByKeyboard: false,
draggable: true,
buttons: [{
label: 'Yes',
action: function (dialog) {
defer.resolve(true);
dialog.close();
}
}, {
label: 'No',
action: function (dialog) {
defer.resolve(false);
dialog.close();
}
}],
close: function (dialog) {
dialog.remove();
}
});
return defer.promise();
}
function bsAlert(error, message) {
BootstrapDialog.show({
type: error ? BootstrapDialog.TYPE_DANGER : BootstrapDialog.TYPE_SUCCESS,
title: error ? "Error" : "Success",
message: message,
closeByBackdrop: false,
closeByKeyboard: false,
draggable: true,
buttons: [{
label: 'OK',
action: function (d) {
d.close();
}
}]
});
}
和使用它(几乎相同的方式)
bsConfirm("Are you sure Bootstrap is what you wanted?").then(function (answer) {
if (answer) {
bsAlert("Well done! You have made the right choice");
} else {
bsAlert("I don't like you!");
}
});
关于javascript - jQuery 延迟和对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13758928/