Bootstrap对话框确认onclick确认事件

我正在尝试编写自己的方法来调用BootstrapDialog并创建一个确认弹出窗口。 如果用户选择“确认”,则该方法将返回true并继续调用jsf操作。 我到目前为止的代码:

/** * Confirm window * * @param {type} message * @param {type} callback * @returns {undefined} */ BootstrapDialog.confirmation = function(title, message) { var callback = function(result) { console.log(result); return result; }; var b = new BootstrapDialog({ title: title, message: message, closable: false, data: { 'callback': callback }, buttons: [{ label: 'Cancel', action: function(dialog) { typeof dialog.getData('callback') === 'function' && dialog.getData('callback')(false); dialog.close(); } }, { label: 'Continue', cssClass: 'btn-primary', action: function(dialog) { typeof dialog.getData('callback') === 'function' && dialog.getData('callback')(true); dialog.close(); } }] }).open(); return callback; }; 

我这样称呼js:

  

弹出对话框就好了,问题是它没有返回true / false。 我在这个例子之后模拟了我的js: http : //nakupanda.github.io/bootstrap3-dialog/

我不认为你真的想扩展BootstrapDialog 。 而且我不希望对话框返回任何内容,因为它将以异步方式运行(它必须等待用户通过按下按钮来操作。)所以你需要通过它的回调与它进行交互。

我不熟悉bootstrap3-dialog超出了我刚刚看到的浏览Github页面的内容,因此它可以为您提供额外的回调或事件,以便更加整洁地完成此操作。 但我认为这将大致完成你所追求的目标:

 function letUserExit() { // Add code here to redirect user where she expects to go // when pressing exit button or to submit a form or whatever. } var exitConfirmDialog = new BootstrapDialog({ title: 'Data unsaved', message: 'Are you sure you want to continue?', closable: false, buttons: [ { label: 'Cancel', action: function(dialog) { dialog.close(); } }, { label: 'Continue', cssClass: 'btn-primary', action: function(dialog) { letUserExit(); } } ] }); // Add event to invoke dialog to your exit button here $('button.exit').on('click', function() { // Show or open? Not quite sure what difference is. Use the one // that's most appropriate. exitConfirmDialog.show(); // Stop any events (like a form being submitted or user being // redirected.) Need to wait until user responds to modal and // have event take place then. return false; }); 

我是Bootstrap的新手,仅在两小时前发现了bootstrap-dialog,所以我的回答只值得它的价值……(法语说,它是如何用英语翻译的?)

我很快遇到了类似的问题:如何使用一个很好的bootstrap确认在提交按钮点击时替换丑陋的javascript确认API?…

正如KLenwel所说,confirm是一个同步API,bootstrap-dialog是一个异步的对话框。 你的线程不等待用户回复(或不回复)。

我使用辅助按钮弹出确认对话框。 真正的提交按钮被隐藏。 Bootstrap对话框确认触发了提交按钮的单击,同时保留了javascript提交处理程序和发送到服务器的http标头。

此代码示例是ASP.Net Webform页面的摘录,使用数据绑定为隐藏按钮提供唯一的css类名称,以便javascript对话框回调可以轻松触发它。 我确实使用了CssClass属性,而不是使用id属性。 这仅适用于ASP.Net的原因。

  ').click(); } )" /> 

同样的事情,在纯HTML中,ASP.Net WebForms独立(使用控件的id):

   

我有同样的问题,但我使用eval()解决了。 function是

 function showConfirmBootstrap(type,title,mesagge, myFunction){ BootstrapDialog.confirm({ size: "size-small", type: "type-"+type, title: title, message: mesagge, cssClass: 'delete-row-dialog', closable: true, callback: function(result) { if(result) { eval(myFunction); } } }); } 

当结果为true时,应用程序将执行myFunction。 你怎么看,myFunction是main函数中的一个参数。

您可以将类型bootstrap,title和message作为参数发送。

一个例子是:

 showConfirmBootstrap("danger","Error","Do you want do this function?", "alert(5)")