简体   繁体   English

重新加载页面后显示警报

[英]Show alert after reloading page

I have tried this function but it displays the alert then refreshes the form.我试过这个 function 但它显示alert然后刷新表单。 I want something that refreshes the page and then displays the alert .我想要刷新页面然后显示alert的东西。 How can I do that?我怎样才能做到这一点?

 $(document).on('click', '#insert', function(){
   var address = $('#to_address').val();
    var amount  =  $('#amount').val();
    var this_email  =  $('#this_email').val();
    var this_bal  =  $('#this_bal').val();
   if(address != '' && amount != '' && this_email != '' && this_bal != '' )
   {
    $.ajax({
     url:"WithdrawChk.php",
     method:"POST",
     data:{address:address, amount:amount, this_email:this_email, this_bal:this_bal},
     success:function(data)
     {
      $('#alert_message').html('<div class="alert-success">'+data+'</div>');
     }
    });
    setInterval(function(){
     $('#alert_message').html('');
    }, 5000);
   }
   else
   {
    alert("All Fields are required");
   }
  });

You can do something like this你可以做这样的事情

        success: function(data) {
          window.sessionStorage.setItem("successData" , JSON.stringify(data));
          window.location.reload();            
       }

$(document).ready(function(){
    if(window.sessionStorage.getItem("successData")){
      $('#alert_message').html('<div class="alert-success">' + JSON.parse(window.sessionStorage.getItem("successData")) + '</div>');             
}
})

Make sure to clear the session storage before the ajax call.确保在调用 ajax 之前清除 session 存储。 And you can store only string values in the local or session storage so you have to stringify the json object to store it in session storage, and then if you want to use it, you have to parse that. And you can store only string values in the local or session storage so you have to stringify the json object to store it in session storage, and then if you want to use it, you have to parse that.

As per Rorys suggestion you can store the data in localStorage and check for if it exists on the jQuery page ready function.根据 Rorys 的建议,您可以将数据存储在 localStorage 中,并检查它是否存在于 jQuery 页面就绪 function 上。

 var localStorage = window.localStorage;

 $(function(){
        var data = localStorage.getItem("data");
        if(data !== null){
            $('#alert_message').html('<div class="alert-success">' + JSON.parse(data) + '</div>');
            localStorage.removeItem("data");
        }
 });


  success: function(data) {
      localStorage.setItem("data", JSON.stringify(data);
      window.location.reload();
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM