0

我使用 BootstrapDialog 库来显示模式对话框、警报和确认消息我在另一个函数中使用BootstrapDialog.confirm方法来重用按钮文本、消息文本等一些参数....为此我编写了下面的代码用于测试目的,但是函数不返回任何东西总是返回undefined

 <head>
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap3-dialog/1.34.7/css/bootstrap-dialog.min.css" rel="stylesheet"/>
 <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap3-dialog/1.34.7/js/bootstrap-dialog.min.js"></script>
<script>
function callconfirm()
{
   // var isConfirmed
     BootstrapDialog.confirm({
            title: 'WARNING',
            message: 'Warning! Drop your banana?',
            type: BootstrapDialog.TYPE_WARNING, // <-- Default value is BootstrapDialog.TYPE_PRIMARY
            closable: false, // <-- Default value is false
            draggable: false, // <-- Default value is false
            btnCancelLabel: 'Do not drop it!', // <-- Default value is 'Cancel',
            btnOKLabel: 'Drop it!', // <-- Default value is 'OK',
            btnOKClass: 'btn-warning', // <-- If you didn't specify it, dialog type will be used,
            callback: function(result) {
                // result will be true if button was click, while it will be false if users close the dialog directly.
                if(result) {
                  return true;
                }else {
                    return false;
                }
            }
        });

}

function checkConfirm()
{
    var v=callconfirm();
    console.log(v);
}
</script>
</head>
<body>
<button type="button"  value="click" onclick="checkConfirm()" style="height: 50px;width: 50px" />
</body>
4

4 回答 4

0

<!DOCTYPE html>
<meta charset="utf-8">
<head>
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap3-dialog/1.34.7/css/bootstrap-dialog.min.css" rel="stylesheet"/>
 <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap3-dialog/1.34.7/js/bootstrap-dialog.min.js"></script>
<script>
function callconfirm(cb)
{
    var isConfirmed
     BootstrapDialog.confirm({
            title: 'WARNING',
            message: 'Warning! Drop your banana?',
            type: BootstrapDialog.TYPE_WARNING, // <-- Default value is BootstrapDialog.TYPE_PRIMARY
            closable: false, // <-- Default value is false
            draggable: false, // <-- Default value is false
            btnCancelLabel: 'Do not drop it!', // <-- Default value is 'Cancel',
            btnOKLabel: 'Drop it!', // <-- Default value is 'OK',
            btnOKClass: 'btn-warning', // <-- If you didn't specify it, dialog type will be used,
            callback: cb
        });

}
</script>
</head>
<body>
<button type="button"  value="click" onclick="javascript:callconfirm(console.log)" style="height: 50px;width: 50px" />
</body>
 

于 2017-02-15T16:54:10.870 回答
0

BootstrapDialog.confirm调用该操作的回调。console.log因此,如果您可以在回调中执行,那就更好了。该函数将立即返回,并且在用户实际单击“确定”或“取消”按钮之前,您将无法依赖结果值。

工作代码:

<head>
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap3-dialog/1.34.7/css/bootstrap-dialog.min.css" rel="stylesheet"/>
 <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap3-dialog/1.34.7/js/bootstrap-dialog.min.js"></script>
<script>
function callconfirm()
{
    var isConfirmed
     BootstrapDialog.confirm({
            title: 'WARNING',
            message: 'Warning! Drop your banana?',
            type: BootstrapDialog.TYPE_WARNING, // <-- Default value is BootstrapDialog.TYPE_PRIMARY
            closable: false, // <-- Default value is false
            draggable: false, // <-- Default value is false
            btnCancelLabel: 'Do not drop it!', // <-- Default value is 'Cancel',
            btnOKLabel: 'Drop it!', // <-- Default value is 'OK',
            btnOKClass: 'btn-warning', // <-- If you didn't specify it, dialog type will be used,
            callback: function(result) {
                // result will be true if button was click, while it will be false if users close the dialog directly.
                if(result) {
                  isConfirmed=true;
                   console.log(isConfirmed);
                }else {
                    isConfirmed=false;
                     console.log(isConfirmed);
                }
            }
        });

}
</script>
</head>
<body>
<button type="button"  value="click" onclick="callconfirm()" style="height: 50px;width: 50px" />
</body>

于 2017-02-15T16:59:16.757 回答
0

感谢 Agalo 和 Tareq 的建议和帮助,我在下面发布了解决方案代码,这可能对其他人有所帮助。

<head>
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap3-dialog/1.34.7/css/bootstrap-dialog.min.css" rel="stylesheet"/>
 <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap3-dialog/1.34.7/js/bootstrap-dialog.min.js"></script>
<script>

function callconfirm(cb)
{
   // var isConfirmed
     BootstrapDialog.confirm({
            title: 'WARNING',
            message: 'Warning! Drop your banana?',
            type: BootstrapDialog.TYPE_WARNING, // <-- Default value is BootstrapDialog.TYPE_PRIMARY
            closable: false, // <-- Default value is false
            draggable: false, // <-- Default value is false
            btnCancelLabel: 'Do not drop it!', // <-- Default value is 'Cancel',
            btnOKLabel: 'Drop it!', // <-- Default value is 'OK',
            btnOKClass: 'btn-warning', // <-- If you didn't specify it, dialog type will be used,
            callback:cb /*function(result) {
                // result will be true if button was click, while it will be false if users close the dialog directly.
                if(result) {
                  return true;
                }else {
                    return false;
                }
            }*/
        });

}

function b1checkConfirm(result)
{
    //callconfirm();
    console.log("B1 click and result is "+result);
}
function b2checkConfirm(result)
{
    //callconfirm();
    console.log("B2 click and result is "+result);
}
</script>
</head>
<body>
<button type="button"  onclick="callconfirm(b1checkConfirm)" style="height: 50px;width: 50px" >B1</button>
<button type="button"  onclick="callconfirm(b2checkConfirm)" style="height: 50px;width: 50px" >B2</button>
</body>
于 2017-02-15T18:54:21.027 回答
0

我从这个插件的Dante创建者那里得到了回复,我分享了这个在额外参数下工作正常的波纹管。

https://github.com/nakupanda/bootstrap3-dialog/issues/329

于 2017-02-21T16:18:48.390 回答