改变sweetalert内部的变量

时间:2016-01-17 17:40:49

标签: javascript sweetalert

我想根据用户在确认时是否按下确定来更改变量。然后我将变量传递给ajax代码。

基本上,当我在上面的函数中写入ajax 3次(!)时它会起作用。

我尝试通过更改swal中的变量update_plan然后在swal之外运行ajax来简化它,但是swal并不像普通警报那样工作,因为代码继续运行并且不会停止弹出警报 - 而不是停止运行代码的常规警报。有什么方法可以提高效率吗?

var update_plan = 'no';

if($('#form_updated').val() == 'yes'){      

  swal({ 
    title: '',  
    text: "Do you want to save this plan for all future refills?",   
    type: "warning",   
    showCancelButton: true,   
    confirmButtonColor: "#39b54a",   
    confirmButtonText: "Yes, save it!",  
    animation: 'slide-from-bottom ', 
    closeOnConfirm: false
    }, 
    function(isConfirm){
      if(isConfirm){
        swal('Saved!','You have switched plans.', 'success');
        update_plan = 'yes';

        $.ajax({
          type: "POST",       
          url: "front_end_functions.php",       
          data: "function=save_client_data&cNumber="+cNumber+"&carrier="+carrier+"&plan="+plan+"&update_plan="+update_plan+"&random_2="+random_2,       
          success: function(msg){  
            if(msg == 'error2'){           
              swal('','We have detected a problem. Please try again later.', 'error');
              return false;        
            }else{                    
              var jsArray = JSON.stringify(msg);           
              $('#orange_bg').toggle('slow');           
              get_blue_div(jsArray);                 
            }        
          }
        });
      }else{

        $.ajax({
          type: "POST",       
          url: "front_end_functions.php",       
          data: "function=save_client_data&cNumber="+cNumber+"&carrier="+carrier+"&plan="+plan+"&update_plan="+update_plan+"&random_2="+random_2,       
          success: function(msg){  
            if(msg == 'error2'){           
              swal('','We have detected a problem. Please try again later.','error');
              return false;        
            }else{                    
              var jsArray = JSON.stringify(msg);           
              $('#orange_bg').toggle('slow');           
              get_blue_div(jsArray);                 
            }      
          } 
        });
      }  
  });

  }else{

    $.ajax({
    type: "POST",       
    url: "front_end_functions.php",       
    data: "function=save_client_data&cNumber="+cNumber+"&carrier="+carrier+"&plan="+plan+"&update_plan="+update_plan+"&random_2="+random_2,       
    success: function(msg){  
      if(msg == 'error2'){           
      swal('','We have detected a problem. Please try again later.','error');
      return false;        
      }else{                    
        var jsArray = JSON.stringify(msg);           
        $('#orange_bg').toggle('slow');           
        get_blue_div(jsArray);                 
      }      
    }
  });
}

1 个答案:

答案 0 :(得分:0)

您还需要在回调中读取update_plan的值。

程序不会停止等待用户的响应(如内置的alert())。