0

下面是我的编码。请告知为什么甜蜜警报只弹出不到 1 秒然后自动重定向到另一个页面。

来自 HTML:

$.ajax({
  url: "/ABC/AddST/",
  data: {
    "username": $("#username").val(),
    "fullname": $("#fullname").val()
  },
  type: "POST"
}).success(function(data) {
  swal({
    title: "Done!",
    text: "Sales Team was successfully added!",
    type: "success"
  }, function() {
    window.location.href = '/ABC/STList';
  });
}).error(function(data) {
  swal("Oops", "We couldn't connect to the server!", "error");
});

从控制器:

[HttpPost]
public ActionResult AddSalesTeam(string username, string fullname)
{
    var st = new Sales();

    if (ModelState.IsValid)
    {
        st.Username = username;
        st.FullName = fullname;
        db.User.Add(st);
        db.SaveChanges();
        return RedirectToAction("SalesTeamList");
    }
    return View();
}
4

3 回答 3

1

据我从问题中了解到,您想显示警报,然后当用户单击确认按钮时,然后重定向到/ABC/STList页面(而不是自动重定向用户),对吗?如果是这样。你试过这个吗?:

swal({
  title: "Done!",
  text: "Sales Team was successfully added!",
  type: "success",
  showCancelButton: false,
  confirmButtonText: "Ok",   
  closeOnConfirm: false
}, function() {
  window.location.href = '/ABC/STList';
});

=========================

编辑:如果需要,您可以删除该行,然后单击确定closeOnConfirm: false按钮后弹出窗口将关闭(但您仍将被重定向)。只是因为最终用户可以更好地看到单击按钮后弹出窗口已关闭。

于 2016-07-08T17:08:07.487 回答
1

你需要给它一个计时器。

swal({
  title: "Done!",
  text: "Sales Team was successfully added!",
  type: "success",
  timer: 1000, // Wait 1 second
  showConfirmButton: false // No reason to show the button
}, function() {
  window.location.href = '/ABC/STList';
});
于 2016-07-08T16:54:32.997 回答
0

在 $ajax 函数之前和之后添加 e.preventDefault() 即可。

e.preventDefault();  
$.ajax({
  url: "/ABC/AddST/",
  data: {
    "username": $("#username").val(),
    "fullname": $("#fullname").val()
  },
  type: "POST"
}).success(function(data) {
  swal({
    title: "Done!",
    text: "Sales Team was successfully added!",
    type: "success"
  }, function() {
    window.location.href = '/ABC/STList';
  });
}).error(function(data) {
  swal("Oops", "We couldn't connect to the server!", "error");
});
e.preventDefault(); 
于 2016-07-09T11:52:59.407 回答