我有一个jquery ui对话框我想用来提示用户确认删除.当用户按下"是"或"否"时,我需要返回"True"或"False"以继续执行一些javascript.下面的代码的问题是当对话框显示它立即执行"return true"时 但用户还没按下"是"按钮呢?
我究竟做错了什么?
HTML:
<div id="modal_confirm_yes_no" title="Confirm"></div>
Run Code Online (Sandbox Code Playgroud)
Javascript电话:
$("#modal_confirm_yes_no").html("Are you sure you want to delete this?");
var answer = $("#modal_confirm_yes_no").dialog("open");
if (answer)
{
//delete
}
else
{
//don't delete
}
Run Code Online (Sandbox Code Playgroud)
Jquery对话框:
$("#modal_confirm_yes_no").dialog({
bgiframe: true,
autoOpen: false,
minHeight: 200,
width: 350,
modal: true,
closeOnEscape: false,
draggable: false,
resizable: false,
buttons: {
'Yes': function(){
$(this).dialog('close');
return true;
},
'No': function(){
$(this).dialog('close');
return false;
}
}
});
Run Code Online (Sandbox Code Playgroud) 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: