【问题标题】:Reload page after message is shown, jQuery显示消息后重新加载页面,jQuery
【发布时间】:2012-01-14 08:29:15
【问题描述】:

现在我有一个用户输入信息的表单。它比处理 bu jQuery ajax 函数并设置为 return false; 因此在用户提交表单后不会发生页面重新加载。 虽然我需要重新加载页面,但如果我删除 return false; 它会在显示成功消息之前刷新页面(此消息在用户提交数据后显示)。

     $.ajax({
      type: "POST",
      url: "scripts/process.php",
      data: dataString,
      success: function() {
       $("#st_message").html("<p> Your article was successfully added!</p>");
       //I need to reload page after the above message is shown
      }
     });
    return false;

那么我如何在显示&lt;p&gt; Your article was successfully added!&lt;/p&gt; 消息后重新加载页面,稍微延迟 2 到 3 秒,这样用户才能真正阅读消息。

【问题讨论】:

    标签: jquery ajax reload


    【解决方案1】:

    您可以使用setTimeout() 函数添加延迟,例如:

    // This will reload the page after a delay of 3 seconds
    window.setTimeout(function(){location.reload()},3000)
    

    满足您的需求:

    $.ajax({
          type: "POST",
          url: "scripts/process.php",
          data: dataString,
          success: function() {
               $("#st_message").html("<p> Your article was successfully added!</p>");
               window.setTimeout(function(){location.reload()},3000)
          }
    });
    return false;
    

    Working Example

    【讨论】:

    • 由于某种原因它不起作用,可能是因为return false?
    【解决方案2】:

    用类似的东西来做:

    function delayedRedirect(){
        window.location = "/index.php"
    }
    
    setTimeout('delayedRedirect()', 3000)
    

    setTimeout 用于延迟重定向函数调用,在这种情况下,您可以将某处重定向到新 URL(以防万一您也需要)。

    否则使用location.reload()重新加载页面。

    【讨论】:

      【解决方案3】:

      使用setTimeout 方法获取延迟,使用reload 方法重新加载页面。请注意reload 调用中的true 参数,以确保页面实际重新加载,而不仅仅是从缓存中重新绘制。

      window.setTimeout(
        function(){
          location.reload(true)
        },
        3000
      );
      

      【讨论】:

      • 由于某种原因它不起作用,可能是因为return false?
      • 不,只有当您尝试立即重新加载页面时才会出现问题。此外,按照您在另一条评论中的说明更改代码的顺序不会有任何区别。也许是错字?
      【解决方案4】:

      你的意思是像:window.location.reload() in JavaScript

      【讨论】:

      • 由于某种原因它不起作用,可能是因为return false?
      【解决方案5】:

      给它使用

      setTimeout("window.location='yourpage.php'",3000);
      

      在您的代码中:

       $.ajax({
             type: "POST",
             url: "scripts/process.php",
             data: dataString,
             success: function() {
              $("#st_message").html("<p> Your article was successfully added!</p>");
              setTimeout("window.location='yourpage.php'",3000);//reload after 3 sec.
             }
            });
       return false;
      

      【讨论】:

      • 由于某种原因它不起作用,可能是因为return false?
      • 出于某种原因,我需要在 $("#st_message").htm... 之前添加它,现在可以使用了,谢谢 )))
      【解决方案6】:

      使用超时是一个非常糟糕的主意。

      【讨论】:

        【解决方案7】:
         $.ajax({
          type: "POST",
          url: "scripts/process.php",
          data: dataString,
          success: function() {
          var miliseconds = 2000;
        
           $("#st_message").html("<p> Your article was successfully added!</p>");
           setTimeout("window.location=window.location", miliseconds);
           //I need to reload page after the above message is shown
          }
         });
        

        【讨论】:

          【解决方案8】:
          <script type="text/javascript">
           //
           //your action here
           //
           window.setTimeout(function(){self.parent.location="?list"},3000);//after 3 sec go to (?list or example.html page)
           return false;
          </script>
          

          【讨论】:

            【解决方案9】:

            示例:

            success: function(data){
                        $("#loading-img").css("display","none");
                             swal({
                              title: "Félécitations",
                              text: "Votre demande a été transmise au service concerné",
                              icon: "success",
                              button: "OK",
                            });
                        window.setTimeout(function(){location.reload(true)},3000)
                        }
                        
                        });
                        return false;
                        }

            【讨论】:

              猜你喜欢
              • 2020-10-08
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2018-12-16
              • 2015-01-29
              • 1970-01-01
              • 2022-01-15
              • 1970-01-01
              相关资源
              最近更新 更多