簡體   English   中英

使用Sweetalert單擊“確定”后重新加載頁面

[英]reload the page after clicking 'OK" using Sweetalert

成功消息后,嘗試重新加載頁面,但未重新加載。

我有這個代碼

$.ajax({
  type: "post",
  url: '/action.cfm?method=quote',
  data: datastring,
  success: function(data) {
    var valid = $.trim(data);
    if (valid.toLowerCase().indexOf("error") == '-1') {
      localStorage.setItem("swal", swal({
        title: "Good job!",
        text: 'Thanks',
        type: "success",
        showConfirmButton: true
      }).then(function() {
        location.reload();
      }));
      localStorage.getItem("swal");
    } else {
      swal("Oops", data, "error");
    }
  }
});

但我得到一個錯誤

(index):389 Uncaught TypeError: Cannot read property 'then' of undefined
    at Object.success ((index):389)
    at fire (jquery-1.12.4.js:3232)
    at Object.fireWith [as resolveWith] (jquery-1.12.4.js:3362)
    at done (jquery-1.12.4.js:9840)
    at XMLHttpRequest.callback (jquery-1.12.4.js:10311)

確保您使用的是Sweetalert的新版本(下面在jsfiddle中,您可以找到2.1.2)-我收到有關使用不贊成使用的showConfirmButtontype屬性的警告。 這是在jsfiddle中工作的稍微修改的代碼:

$(document).ready(function() {
    $.ajax({
        type: "post",
        url: '/echo/json/',
        data: {},
        success: function(data) {
            var valid = $.trim(data);
            if (valid.toLowerCase().indexOf("error") == '-1') {
                localStorage.setItem("swal", swal({
                    title: "Good job!",
                    text: 'Thanks',
                    icon: "success",
                    button: true
                }).then(function() {
                    console.log('sth');
                    location.reload();
                }));
                localStorage.getItem("swal");
            } else {
                swal("Oops", data, "error");
            }
        }
    });
});

https://jsfiddle.net/wlecki/rwc58h17/

但是總的來說,如果必須將其存儲在瀏覽器的本地存儲中,建議您僅在其中存儲一個Sweetalert選項,如果要顯示Sweetalert,請使用它們:

$(document).ready(function() {
    $.ajax({
            type: "post",
            url: '/echo/json/',
            data: {},
            success: function(data) {
                var valid = $.trim(data);
                if (valid.toLowerCase().indexOf("error") == '-1') {
                    localStorage.setItem("swal", JSON.stringify({
                        title: "Good job!",
                        text: 'Thanks',
                        icon: "success",
                        button: true
                    }));

                    swal(JSON.parse(localStorage.getItem("swal"))).then(() => location.reload());
            } else {
                swal("Oops", data, "error");
            }
        }
    });
});

https://jsfiddle.net/wlecki/71vzasxh/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM