2016-11-03 60 views
0

我一直坚持这几天。我使用的是sweetalert2,我试图完成的是根据给定的答案触发一个javascript函数,所以如果我点击确定,它会触发一件事,如果我点击取消,它会执行其他操作。我可以让它工作,但它似乎在甜蜜警报完成之前触发该功能。这里是一个代码示例:响应甜蜜提示2后触发一个函数

<script> 
    function validateSubmit(a,b,c){ 
     var mode = a; 
     var info = b; 
     var trunk = c; 

     var iteration = baseName(info); 

     if (mode === 'Update' && info != trunk){ 
      confirmGetMessage(iteration); 
     } 
     else { 
      alert('seems to work') 
     } 
    } 

    function baseName(str) { 
     var base = new String(str).substring(str.lastIndexOf('/') + 1); 
     if(base.lastIndexOf(".") != -1)  
     base = base.substring(0, base.lastIndexOf(".")); 
     return base; 
    } 

    function trythis(){ 
     alert('made it right here!'); 
    } 

    function confirmGetMessage(info){ 
     var message = "<h3>" + info + "</h3><br/>Or Revert Back to Trunk?";    var contmessage = "Updating " + info; 

     swal({ 
      title: "Update Branch?", 
      html: message, 
      type: "question", 
      showCancelButton: true, 
      cancelButtonText: "Switch To Trunk", 
      cancelButtonColor: "#0080FF", 
      confirmButtonColor: "#DD6B55", 
      confirmButtonText: "Continue Update", 
      closeOnConfirm: false, 
      closeOnCancel: false 
     }).then(
      function(result){ 
       swal({ 
        text: contmessage, 
        timer: 1400, 
        showConfirmButton: false 
       }), alert('work please'); 
      }, function(dismiss) { 
       swal({ 
        text: 'Switching to Trunk', 
        timer: 1400, 
        showConfirmButton: false 
       }); 
      } 
     ); 
    } 
</script> 

所以,如果你运行这个代码,弹出警告框从sweetalert的消息框。

---------- UPDATE ---------------

运行代码,像这样似乎越来越近,虽然现在警报消息闭幕消息之前仍然发生,但至少这个时候,我可以看到收消息

function validateSubmit(a,b,c){ 
      var mode = a; 
      var info = b; 
      var trunk = c; 

      var iteration = baseName(info); 

      if (mode === 'Update' && info != trunk){ 
       confirmGetMessage(iteration); 
      } 
      else { 
       alert('seems to work') 
      } 
     } 

     function baseName(str) { 
      var base = new String(str).substring(str.lastIndexOf('/') + 1); 
      if(base.lastIndexOf(".") != -1)  
      base = base.substring(0, base.lastIndexOf(".")); 
      return base; 
     } 

     function trythis(){ 
      alert('made it right here!'); 
     } 

     function confirmGetMessage(info){ 
      var message = "<h3>" + info + "</h3><br/>Or Revert Back to Trunk?"; 
      var contmessage = "Updating " + info; 

      swal({ 
       title: "Update Branch?", 
       html: message, 
       type: "question", 
       showCancelButton: true, 
       cancelButtonText: "Switch To Trunk", 
       cancelButtonColor: "#0080FF", 
       confirmButtonColor: "#DD6B55", 
       confirmButtonText: "Continue Update", 
       closeOnConfirm: false, 
       closeOnCancel: false 
      }).then(function(){ 
       swal({ 
        text: contmessage, 
        timer: 1400, 
        showConfirmButton: false 
       },trythis()) 
      }, function(dismiss){ 
       if (dismiss === 'cancel'){ 
        swal({ 
         text: 'Switching to Trunk', 
         timer: 1400, 
         showConfirmButton: false 
        }) 
       } 
      } 
     )} 

回答

0

您正在阅读的价值为result在创建警报后的第一个函数调用,但你不检查通话的价值。
Sweet alert timer - done function

swal({ 
title: "Are you sure?", 
text: "You will not be able to recover this imaginary file!", 
type: "warning", 
showCancelButton: true, 
confirmButtonColor: "#DD6B55", 
confirmButtonText: "Yes, delete it!", 
cancelButtonText: "No, cancel plx!", 
closeOnConfirm: false, 
closeOnCancel: false 
}, 
function (isConfirm) { 
    if (isConfirm) { 
     swal({ 
      title: "Deleted!", 
      text: "Your row has been deleted.", 
      type: "success", 
      timer: 3000 
     }); 
     function() { 
      location.reload(true); 
      tr.hide(); 
     }; 
    } 
    else { 
     swal("Cancelled", "Your imaginary file is safe :)", "error"); 
    } 
}); 

你需要检查输入是否是确认与否。像这样的东西可能是你想要的,但我没有一个环境来测试它:

swal({ 
     title: "Update Branch?", 
     html: message, 
     type: "question", 
     showCancelButton: true, 
     cancelButtonText: "Switch To Trunk", 
     cancelButtonColor: "#0080FF", 
     confirmButtonColor: "#DD6B55", 
     confirmButtonText: "Continue Update", 
     closeOnConfirm: false, 
     closeOnCancel: false 
    }, 
    function(result){ 
     if(result) 
     { 
      swal({ 
       text: contmessage, 
       timer: 1400, 
       showConfirmButton: false 
      }), alert('work please'); 
     },  
     else{ 
      swal({ 
       text: 'Switching to Trunk', 
       timer: 1400, 
       showConfirmButton: false 
      }); 
     } 
    } 
); 
相关问题