简体   繁体   English

使用iframe内的按钮在iframe外部重新加载div

[英]Using a button inside an iframe to reload a div outside the iframe

I currently have: 我目前有:

<script>
var btn = document.getElementById("doReload");
btn.onclick = function(){

    setTimeout(function(){
        $("#div1").load("News.php");
    }, 5000);
}

</script>
<form action="" method="POST" enctype="multipart/form-data">
<input type="file" name="image" />
<input id="doReload" type="submit"/>
</form>

All of this is loaded in an iframe using src=, When I click the submit button i want it to do a file upload (Which it currently does), then i would like it to reload News.php into Div1 to show the updated file, any ideas on how to do this? 所有这些都使用src =加载到iframe中,当我单击“提交”按钮时,我希望它执行文件上传(当前执行该操作),然后我希望将News.php重新加载到Div1中以显示更新的文件,有关如何执行此操作的任何想法?

In order to trigger #doReload 's click callback, try putting your script after the markup: 为了触发#doReload的click回调,请尝试将脚本放在标记之后:

<form action="" method="POST" enctype="multipart/form-data">
<input type="file" name="image" />
<input id="doReload" type="submit"/>
</form>
<script>
var btn = document.getElementById("doReload");
btn.onclick = function(){

    setTimeout(function(){
        $("#div1").load("News.php");
    }, 5000);
}

</script>

or wrapping it in $( document ).ready(function() { ... }); 或将其包装在$( document ).ready(function() { ... });

But since (afaik) there isn't a callback on html form submission success, you may want to do the post using jQuery's $.post and then use its success callback to retrieve the new data. 但是,由于(afaik)html表单提交成功没有回调,因此您可能希望使用jQuery的$.post ,然后使用其success回调来检索新数据。

Ok, so the problem is that your form is submitting before the timeout executes the function. 好的,所以问题在于您的表单在超时执行功能之前提交。

You should use jQuery to submit the form ( jQuery submit ) with event.preventDefault() to stop the submit-button from actually reloading the page. 您应该使用jQuery来提交带有event.preventDefault()的表单( jQuery Submit),以阻止Submit-button实际重新加载页面。

this can be done by changing the code like so: 这可以通过更改代码来完成,如下所示:

 document.getElementById("doReload").addEventListener("click", function(e) { e.preventDefault(); var form = this.parentElement; var xhr = new XMLHttpRequest(); xhr.open(form.method, form.action); xhr.addEventListener("readystatechange", function() { if (xhr.readyState == 4 && xhr.status == 200) $("#div1").load("News.php"); }) xhr.send(new FormData(form)); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> <form action="upload/" method="POST" enctype="multipart/form-data"> <input type="file" name="image" /> <input id="doReload" type="submit" /> </form> 

(Solved on chat with some other small modifications, this was the final working code though) (通过聊天进行了一些其他小的修改,尽管这是最终的工作代码)

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

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