我正在尝试使用twitter bootstrap模式而不是自定义确认对话框.
我的功能
function getConfirm(confirmMessage){ if ( confirmMessage == undefined){ confirmMessage = ''; } $('#confirmbox').modal({ show:true,backdrop:false,keyboard: false,}); $('#confirmMessage').html(confirmMessage); $('#confirmFalse').click(function(){ $('#confirmbox').modal('hide'); return false; }); $('#confirmTrue').click(function(){ $('#confirmbox').modal('hide'); return true; }); } </script> <div class="modal" id="confirmbox" style="display: none"> <div class="modal-body"> <p id="confirmMessage">Any confirmation message?</p> </div> <div class="modal-footer"> <button class="btn" id="confirmFalse">Cancel</button> <button class="btn btn-primary" id="confirmTrue">OK</button> </div> </div>
调用函数
var confimChange=getConfirm("Do You confirm?"); if(confimChange){ alert('perfect you confirmed') }else{ alert('why didnt you confirmed??') }
问题是getConfirm函数没有返回true或false.
工作代码:
if(receiverListCount > 0){ var confimChange=confirm("Message"); if(confimChange){ resetReceiverList(); }else{ return false; } }
修改后的代码片段更改/不工作
if(activeDiv == '#upload'){ if(listOfSelectedGroups.length> 0|| receiverListCount > 0){ getConfirm("message",function(result){ if(result){ resetReceiverList(); }else{ return false; } }); } }
我决定使用bootboxjs
解决方法
你的方法是错误的,它会执行异步的方式,所以它不会等待模态对话框在返回之前被关闭.尝试这样的东西:
function getConfirm(confirmMessage,callback){ confirmMessage = confirmMessage || ''; $('#confirmbox').modal({show:true,}); $('#confirmMessage').html(confirmMessage); $('#confirmFalse').click(function(){ $('#confirmbox').modal('hide'); if (callback) callback(false); }); $('#confirmTrue').click(function(){ $('#confirmbox').modal('hide'); if (callback) callback(true); }); } getConfirm('Are you sure?',function(result) { // Do something with result... });
注意,但是您只需要初始化一次模式并挂起事件一次.我会给出模态的身体和身份证,并在显示前简单地更改内容.
您还应该将背景设置为true,您需要确认以防止用户访问该页面,直到确认.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。