I am using jQuery UI dialog to display a confirmation dialog when a button is clicked. I want to return true, when OK is clicked and false otherwise.
Associating dialog open call in onClick (as given here, $dialog.dialog('open');) event does not serve the purpose. So, as a workaround, I followed an approach, which is similar to this: http://www.perfectline.co.uk/blog/unobtrusive-custom-confirmation-dialogs-with-jquery. There are two differences between this approach and mine:
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: …Run Code Online (Sandbox Code Playgroud) 您可以在许多帖子(Post 1,Post2)中找到解决方案,但他们的解决方案对我不起作用.
这是我编写的普通jquery对话框.
$("#dialog").dialog({
autoOpen:false,
buttons:{
"ok":function(){
$(this).dialog("close");
return true;
},
"cancel":function(){
$(this).dialog("close"); return false;
}
}
});
Run Code Online (Sandbox Code Playgroud)
我将用代码打开对话框:
var returnVal=$("#dialog").dialog("open");
Run Code Online (Sandbox Code Playgroud)
我需要返回false,如果用户点击"取消"并返回,true如果用户点击"确定".
var returnVal=$("#dialog").dialog("open");
Run Code Online (Sandbox Code Playgroud)
我需要returnVal返回boolean值(true/false),但它返回javascript object.