SweetAlert и перенаправить, если условие в порядке и после успешного сообщения - PullRequest
0 голосов
/ 06 октября 2018

Я использую версию 1 SweetAlert:

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 == "Followup" , мне нужно перенаправить пользователя:

  1. после отображения сообщения об успехе и
  2. после того, как пользователь нажал кнопку ОК.

Как это возможно ??Я новичок и пытаюсь учиться.

Заранее благодарен ..

Ответы [ 2 ]

0 голосов
/ 06 октября 2018
$.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 кстати.

0 голосов
/ 06 октября 2018

Вы можете передать функцию обратного вызова на swal object, например,

swal({
     title: "Report Saved",
     type: "success",
     text: "The user details saved successfully!",
       confirmButtonColor: "#00B4B4"
  },function() {
    if(condition){
       window.location = "redirectURL";
    }
  });
...