简体   繁体   English

在 iframe (javascript/jquery) 中提交后重新加载页面

[英]Reload page after submit in iframe (javascript/jquery)

I have a page with an iframe.我有一个带有 iframe 的页面。 In the iframe there's a submit button.在 iframe 中有一个提交按钮。 When I click the submit button I want the parent window to be updated.当我单击提交按钮时,我希望更新父窗口。 This is what I have done so far, and it works (the javascript is located in the parent window):这是我到目前为止所做的,并且有效(javascript 位于父窗口中):

var myFrame = $('iframe');
myFrame.load(function() {
myFrame.contents().find('#publish').click(function() {

  myFrame.load(function() {
    location.reload();
  });

});
});

The code works.该代码有效。 But I find the reload of the iframe unnecessary.但我发现重新加载 iframe 是不必要的。 Is there a better way to detect when "submit" is completed?有没有更好的方法来检测“提交”何时完成?

(I can't reload the page only by “onclick” or relay on any delay. I must be sure that the submitting of the form is finished) (我不能仅通过“onclick”重新加载页面或延迟任何延迟。我必须确保表单提交已完成)

Assuming publish is unique you can simply do so:假设publish是独一无二的,您可以简单地这样做:

$(document).ready(function(){
  $('#publish').live('click', function(e){
     e.preventDefault();
     //Do whatever you want here
   });
});

Notice the live will bind the even to publish link at runtime.注意live将在运行时绑定甚至publish链接。

You could have the form submission return a block of js:您可以让表单提交返回一个 js 块:

<html>
<script>
 window.parent.document.jQueryFunction();
</script>
</html>

or, if you're open to plug-in, you could use the ajaxSubmit() method of the jQuery Form Plugin .或者,如果您对插件持开放态度,则可以使用jQuery Form Plugin的 ajaxSubmit() 方法。

$myFrame.contents().find('form').ajaxForm({
  success: function() {
     // update parent
  }
});
$("#publish").click(function() {
    $('#inputs').ajaxSubmit({
        success:function(){
            //alert("Reload called");
            parent.location.reload();
        }
    });
 });

This worked.这奏效了。

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

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