2017-02-15 10 views
0

确认我使用BootstrapDialog库来显示模态对话框警报我使用在侧BootstrapDialog.confirm方法的另一功能与像按钮文本,消息文本等某些参数重新使用确认消息。 ...因为我写测试目的的波纹管代码,但功能不返回任何东西总是返回undefinedBootstrapDialog内的另一个功能不工作

<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> 
+0

'callconfirm'不返回任何东西,所以它返回'undefined'。你想要返回'BootstrapDialog.confirm'的值吗? – Agalo

+0

是的,我想得到isConfirmed真或假继续下一步 – matrixwebtech

+0

我发布了一个答案,检查它是否适合你。 – Agalo

回答

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> 
 

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>

+0

打印内部回电正在工作,但如果callconfirm()呼叫方没有返回任何东西,请检查更新的代码。我想在checkConfirm()返回 – matrixwebtech

+0

我没有阅读整个文档,但似乎只有获取结果的方式是指定回调。因此,您可以将回调传递给方法,然后评估结果。 – Agalo

+0

请在初始文章中检查我的更新后的代码,其中id声明了一个回调方法名称cb()并将其分配给回调函数,并且调用了该函数,但是您说过“您可以将回调传递给该方法,然后评估结果”。我无法弄清楚这一点,你能举个例子吗? – matrixwebtech

0

Thankx的AgAlO和塔里克的建议和帮助下,我发布的解决方案代码波纹管可以帮助别的人。

<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>