首先,我是 Javascript/Jquery 的新手,所以这可能是一个愚蠢的问题......
我正在使用 JQuery UI 集合中的对话框。我有一个按钮,单击该按钮时,它会显示一个确认框或一个警告框。我的代码如下...
function checkfn() {
if (document.getElementById('<%=HomeInstSelected.ClientID%>').value == 'False') {
var dialogResult = false;
$("#dialog-confirm").dialog({
resizable: false,
height: 140,
modal: true,
buttons: {
Continue: function () {
dialogResult = true;
$(this).dialog("close");
},
Cancel: function () {
$(this).dialog("close").slideUp();
}
}
});
// alert just debug code!
alert(dialogResult);
return dialogResult;
}
else {
$("#dialog-HomeInstitutionPrompt").dialog({
height: 140,
modal: true
});
}
}
我的问题出在确认部分,确认框似乎没有等我点击继续或取消 - 它只是直接进入警报部分并返回 dialogResult = false。
是否可以在我运行 $('#dialog-confirm') 命令后停止执行 until?
或者,是否可以在 Continue 函数中为 checkfn() 函数返回 true?这样一来,我就根本不需要 dialogResult var 了吗?
最佳答案
我以前没有在 jQuery UI 中使用过 .dialog,但是,通常使用 jQuery,函数是异步运行的。这意味着您需要将要运行的代码放入 Continue 函数中。
一种可能性是向 checkfn 发送一个成功/取消函数,您将从继续和取消函数中调用它。
function checkfn(success, cancel) {
if (document.getElementById('<%=HomeInstSelected.ClientID%>').value == 'False') {
var dialogResult = false;
$("#dialog-confirm").dialog({
resizable: false,
height: 140,
modal: true,
buttons: {
Continue: function () {
if(success) success();
$(this).dialog("close");
},
Cancel: function () {
if(cancel) cancel();
$(this).dialog("close").slideUp();
}
}
});
}
你可以这样调用函数:
checkfn(function () {
alert('success!');
}, function () {
alert('failure!');
});
关于javascript - 我如何让 JQuery 语句等到它完成后再继续?>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5112405/