【问题标题】:How to refresh a page inside another page?如何刷新另一个页面内的页面?
【发布时间】:2017-05-11 10:38:27
【问题描述】:

我有 2 页,在第一页中,我通过 ajax 调用了另一页(第二页)。在第二页中,有一个表单通过 ajax 发送然后重新加载页面一次。但是当第二页刷新时,我的第一页也刷新了。但我不想要它。我只想重新加载我的内页。有没有办法只重新加载内部页面? 这是我的内页代码:

$(document).ready(function() {
  $(document).on('click', '.MyForm button[type=submit]', function(e) {
    e.preventDefault() // To make sure the form is not submitted 
    var $frm = $(this).closest('.MyForm');
    console.log($frm.serialize());
    $.ajax(
        $frm.attr('action'), 
        {
          method: $frm.attr('method'),
          data: $frm.serialize(),
          success: function(data) { $(".error").html(data), window.location.reload();}
   
        }
    );
  });
});
.error{width:200px; height:50px;border:1px solid blue;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.1/jquery.min.js"></script>
  <form class="MyForm" method="post">
    <input type="text" placeholder="name" value="Aynaz" name="a1" />
    <select name="Avg">
      <option value="1">1</option>
      <option value="2">2</option>
    </select>
    <button type="submit">Submit</button>
    <div class="error">Show form success here after page reload</div>
  </form>

【问题讨论】:

  • 内页?您如何将其加载到您的“父页面”中?框架?请澄清
  • 您能否将整个文档显示为 sn-p,可能在 jsfiddle 中?
  • @anu 在 div 中使用简单的 onclick 事件加载页面:$('.div').load('innerpage.htm');
  • 那你为什么不再试一次呢。而不是window.location.reload(); 使用$('.div').html('').load('innerpage.htm');

标签: javascript php jquery html css


【解决方案1】:

window.location.reload() 将始终重新加载整个页面。使用 load() 函数与您最初使用的函数相同来加载内页

 $.ajax(
            $frm.attr('action'), 
            {
              method: $frm.attr('method'),
              data: $frm.serialize(),
              success: function(data) { 
                   $(".error").html(data);
                   $('.div').html('').load('innerpage.htm');// reload the div
              }

            }
        );

【讨论】:

  • 什么是 (.div)?我应该这样做吗?
  • 您在问题下的 cmets 中提到了它。您提到这就是您加载内页的方式。不是这样吗?
  • 不,该代码用于将第二页加载到主页中。我的问题是如何单独重新加载第二页。无需重新加载第一页
  • 是的,这就是您应该如何将第二页重新加载到第一页。 .div 必须是第一页中带有 div 类的 div。如果您感到困惑,请发布您如何加载第二页的代码以及第一页的 html
【解决方案2】:

从表单中删除 method="post"。

【讨论】:

    【解决方案3】:

    您可以在&lt;iframe&gt; 中创建内部页面,然后只能重新加载此部分。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-31
      • 1970-01-01
      • 1970-01-01
      • 2018-12-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多