【问题标题】:How to replace page content on forced reload?如何在强制重新加载时替换页面内容?
【发布时间】:2014-12-11 10:59:10
【问题描述】:

我有一个名为index.html 的页面,其中包含<div id ="header"><div id = "midSection"Header 具有导航元素,当用户单击它们时,我使用 JQuery(AJAX 调用)将不同的 .php 文档加载到 midSection 中。
.php 文件之一具有注册表单,在提交单击时,我成功将数据发布到数据库中。我确实阻止了标准操作,因此在提交数据时不会发生默认重定向。
我想要做的是当数据被发布以将用户重定向到index.html(强制刷新),但header 更改为另一个div(例如headerOnLogin)。反正有这样做吗?


          else {
     $.post($("#regForm").attr("action"),
    $("#regForm :input").serializeArray(),
    function(info){ 
           //php script returns this key if data is inserted into database successfully     
                if (info == 4521789524355214852) { 

                //RELOAD PAGE, REPLACE HEADER IN INDEX.HTML

            }
               else{

                $("#errorArea").empty();
                $("#formPseudoContainer").effect('shake', 750 );
                $("#errorArea").fadeOut('15000', function() { });
                $("#errorArea").fadeIn('15000', function() { });
                $("#errorArea").text(info);
                   setTimeout(function() {
                        $("#errorArea").empty();
                           }, 5000);

               }            

    });
  }

      $("#regForm").submit(function() { // prevents redirecting to another page after submitting    form
         return false;
   });

【问题讨论】:

  • 当这种情况发生时,您正在重新加载整个页面,而不仅仅是 midSection DIV?
  • @Barmar,是的,我想重新加载整个页面,或者至少用另一个header 重定向到同一页面。仅更改 midSection 将是微不足道的,因为我已经实现了这样做的功能 =/
  • 为什么不使用默认操作提交表单,并在完成更新数据库重定向到 index.php/index.html 并使用一些标志/内容?

标签: javascript php jquery html


【解决方案1】:

使用类似的方式重新加载页面:

window.location = "index.html?header=headerOnLogin";

然后让index.html 中的JavaScript 检查window.location.search 以查看它是否有header=headerOnLogin,并显示您想要的新标题DIV。

或者您可以使用index.php,PHP 脚本可以检查$_GET['header']

【讨论】:

    猜你喜欢
    • 2016-02-11
    • 2012-03-26
    • 2014-08-05
    • 2020-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多