2017-02-15 3 views
0

Arbeits I BootstrapDialog Bibliothek verwenden modale Dialoge eine Bestätigungsmeldungen eine Warnmeldung angezeigt I BootstrapDialog.confirm Methode Seite eine andere Funktion mit einigen Parametern wie Button Text wieder zu verwenden verwenden, Nachrichtentext etc. ... für diesen schreibe ich bellow code zu testzwecken, aber die funktion gibt nichts zurück immer undefined.BootstrapDialog bestätigen in einer anderen Funktion nicht

<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' gibt nichts zurück, so ist es die Rückkehr' undefined'. Möchten Sie stattdessen den Wert von 'BootstrapDialog.confirm' zurückgeben? – Agalo

+0

Ja, ich möchte erhalten bestätigt oder falsch, um nächsten Schritt – matrixwebtech

+0

Ich habe eine Antwort geschrieben, überprüfen Sie, ob es für Sie funktioniert. – Agalo

Antwort

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

Die BootstrapDialog.confirm würde den Rückruf auf die Aktion aufrufen. Also, es ist besser, wenn Sie die console.log im Callback stattdessen tun können. Die Funktion würde sofort zurückkehren und Sie könnten sich nicht auf den Ergebniswert verlassen, bis der Benutzer tatsächlich auf OK oder Abbrechen klickt.

Arbeitscode:

<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

Drucken innerhalb Callback funktioniert, aber wenn callconfirm() rief Seite ist nicht zurück, überprüfen Sie bitte aktualisierten Code. Ich möchte die Rückkehr in checkConfirm() – matrixwebtech

+0

Ich habe nicht die gesamte Dokumentation gelesen, aber es scheint, dass das einzige Um das Ergebnis zurückzuerhalten, geben Sie den Rückruf an. Sie können also den Callback an die Methode übergeben und das Ergebnis auswerten. – Agalo

+0

überprüfen Sie bitte meinen aktualisierten Code in der ursprünglichen Post, wo ID eine Callback-Methode Name cb() deklarieren und Callback zuweisen und das heißt, aber Sie sagte dann "Sie können den Rückruf an die Methode übergeben und dann das Ergebnis auswerten." Ich kann das nicht herausfinden, kannst du bitte ein Beispiel geben? – matrixwebtech

0

Thankx Agalo und Tareq für Anregung und Hilfe poste ich die Lösung Code unten, die sonst jemand helfen kann.

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