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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5695380/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 18:02:28  来源:igfitidea点击:

Reload page after submit in iframe (javascript/jquery)

javascriptjqueryiframesubmitreload

提问by Hakan

I have a page with an iframe. In the iframe there's a submit button. 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):

我有一个带有 iframe 的页面。在 iframe 中有一个提交按钮。当我单击提交按钮时,我希望更新父窗口。这是我到目前为止所做的,并且有效(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. Is there a better way to detect when "submit" is completed?

该代码有效。但我发现重新加载 iframe 是不必要的。有没有更好的方法来检测“提交”何时完成?

(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”或延迟任何延迟来重新加载页面。我必须确保表单提交已完成)

回答by Naveed Ahmad

Assuming publishis unique you can simply do so:

假设publish是唯一的,您可以简单地这样做:

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

Notice the livewill bind the even to publishlink at runtime.

请注意,livepublish在运行时将偶数绑定到链接。

回答by two7s_clash

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
  }
});

回答by Preeti

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

This worked.

这奏效了。