SweetAlert并重定向,如果条件还可以,并且在成功消息之后

时间:2018-10-06 06:58:18

标签: javascript jquery sweetalert

我正在使用SweetAlert的版本1:

https://github.com/errakeshpd/sweetalert-1

我的示例ajax代码如下,我在其中保存用户数据。

$.ajax({
type: "POST",
url: "test.php",
data: {name1:name,status1:status},
cache: false,
success: function(result){
$('#submit').prop('disabled', true);
swal({
title: "Report Saved",
type: "success",
text: "The user details saved successfully!",
confirmButtonColor: "#00B4B4"
  });
},//success
error: function(result) 
{   
swal({
title: "Save failed",
type: "warning",
text: "We are unable to save data due to technical reasons!",
confirmButtonColor: "#00B4B4"
   });
 }  
});

如果status1 data ==“ Followup” ,我需要重定向用户:

  1. 显示成功消息后 和
  2. 用户单击“确定”按钮后。

那怎么可能?我是新手,正在尝试学习。

先谢谢您。

3 个答案:

答案 0 :(得分:1)

您可以像这样将回调函数传递给$(document).ready(function () { var clone_index = 0; $('.start_time').timepicker({});//For first time on page load $("#add_period_break").click(function () { clone_index = clone_index + 1; $(this).parent().before($("#clone").clone().attr("id", "clone" + clone_index)); $("#clone" + clone_index).css("display", "inline"); $("#clone" + clone_index + " :input").each(function () { $(this).attr("name", $(this).attr("name") + clone_index); $(this).attr("id", $(this).attr("id") + clone_index); $("#countform").val(clone_index); }); $('.start_time').timepicker({});///to apply timepicker on newly added textbox }); });

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/timepicker/1.3.5/jquery.timepicker.js"></script>

<div id='clone'>
    <div class="form-group">
        <label for="exampleInputEmail1">Start Time</label>
        <input type="text" name="start_time" id="start_time" class="form-control start_time" readonly="readonly" value="" placeholder="Start Time">
    </div>
</div>

<div id="box-footer">
    <button type="button" class="btn btn-success" name="add_period_break" id="add_period_break"><i class="glyphicon glyphicon-plus"></i> Add Periods/ Breaks</button>
    <button type="submit" class="btn btn-primary"> Submit</button>
</div>

答案 1 :(得分:0)

$.ajax({
   type: "POST",
   url: "test.php",
   data: {name1:name,status1:status},
   cache: false,
   success: function(result){
      $('#submit').prop('disabled', true);
      swal({
         title: "Success!",
         text: "Please click yes to reload or relocate to the other page.",
         type: "success",
         confirmButtonColor: "#DD6B55",
         confirmButtonText: "Yes",
         }).then((result) => {
             if(result.value){
                    //this is your success swal, after clicking the yes button, it will reload or go to the other page.
                    location.reload(); // this is your location reload.
                    window.location.href='somepage.html'; // this is your relocate to other page.
             }
        })
   },
   error: function(result) {   
      swal({
         title: "Save failed",
         type: "warning",
         text: "We are unable to save data due to technical reasons!",
         confirmButtonColor: "#00B4B4"
      });
   }  
});

我使用Sweetalert 2 btw。

答案 2 :(得分:-1)

swal(
title: "Save failed",
type: "warning",
text: "We are unable to save data due to technical reasons!",
confirmButtonColor: "#00B4B4").then(() => { window.location = "redirectUrl" });