简体   繁体   English

为什么单击按钮后,Firefox和IE无法刷新页面?

[英]Why firefox and IE not refresh page after click button?

Why firefox and IE not refresh page after click button ? 为什么单击按钮后,Firefox和IE无法刷新页面?

After click button , it's will show "OK." 点击按钮后,它将显示“确定”。 and then refresh page. 然后刷新页面。

I test on chrome, opera, safari it's OK. 我在Chrome,Opera,Safari上进行了测试,还可以。 but on firefox and IE not refresh How can i do ? 但是在Firefox和IE上无法刷新怎么办?

test_1.php test_1.php

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>

<form id="myForm" action="test_2.php" method="post">
    <button id="sub">Send</button>
</form>
<span id="result"></span>

<script type="text/javascript">
$("#sub").click( function() {
 $.post( $("#myForm").attr("action"), 
         $("#myForm :input").serializeArray(), 
         function(info){ $("#result").html(info); 
   });
 clearInput();
});

$("#myForm").submit( function() {
  return false; 
});
</script>

test_2.php test_2.php

<?php
  echo "OK.";
  echo "<META HTTP-EQUIV = 'Refresh' Content = '2'>";
?>

Why do you want to reload the page if you use AJAX? 如果使用AJAX,为什么要重新加载页面? It completely defies the need. 它完全没有必要。 The meta tag lives in the head and is not executed from an Ajax call in most browsers 在大多数浏览器中,meta标记位于头部,并非通过Ajax调用执行

In test2.php you can do 在test2.php中,您可以执行

// save your data here
$status = someFunctionReturningSomeString(parameters);
header("location: test_1.php?status=$status"); 

and back in test1.php, test that status and show it to user: 然后返回test1.php,测试该状态并将其显示给用户:

<? 
$status = $_GET["status"]; // perhaps some cleaning is needed too 
if (isset($status)) { 
   echo "<script>$(function() $("#result").html('".$status."')});</script>";
} ?>
<form id="myForm" action="test_2.php" method="post">
    <input type="submit" value="Send"/>
</form>

NOTE : Do NOT echo anything before a header or you will get Cannot modify header information - headers already sent 注意 :不要在标头之前回显任何内容,否则您将得到无法修改标头信息-标头已发送


If you MUST reload you can better do the following than return a meta tag 如果必须重新加载,则比返回元标记更好地执行以下操作

$("#myForm").on("submit", function(e) {
  e.preventDefault(); // cancel the form
  $.post( $("#myForm").attr("action"), 
     $("#myForm :input").serializeArray(), 
     function(info){ $("#result").html(info); 
     // allow the user to see the result perhaps?
     setTimeout(function() { location.reload(1)},3000); 
  });
  clearInput();
});

for you are working with jQuery you could do this: 因为您正在使用jQuery,您可以执行以下操作:

<script>
 $(document).ready(function() {
     location.reload();
 });
</script>

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

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