发布到 iframe 时同步表单提交
我有一个表单,可将数据发布到隐藏的 以防止页面更改。不过,我希望“保存”按钮执行两个连续的
submit()
,一个接一个。
<form name="updatedocform" id="updatedocform" method="post" action="http://foo.bar" enctype="multipart/form-data" target="hiddenframe">
...
<input type="submit" onclick="save(); return false;" value="Save" />
</form>
<iframe name="hiddenframe" id="hiddenframe"></iframe>
JavaScript:
function save() {
document.updatedocform.submit();
// change some form values here
// alert("paused"); // if this alert is here, everything works fine
document.updatedocform.submit();
}
但是,在第一个 submit()
加载完成之前,第二个 submit()
被调用。有没有办法让第二次提交等待第一个完成?
我可以使用 setTimeout()
但我想要一个不依赖于网络速度的更干净的解决方案。
I have a form which POSTs data to a hidden <iframe>
to prevent the page from changing. However I would like the Save button to perform two consecutive submit()
s, one after the other.
<form name="updatedocform" id="updatedocform" method="post" action="http://foo.bar" enctype="multipart/form-data" target="hiddenframe">
...
<input type="submit" onclick="save(); return false;" value="Save" />
</form>
<iframe name="hiddenframe" id="hiddenframe"></iframe>
JavaScript:
function save() {
document.updatedocform.submit();
// change some form values here
// alert("paused"); // if this alert is here, everything works fine
document.updatedocform.submit();
}
However the second submit()
is being called before the first one is finished loading. Is there a way to make the second submit wait for the first one to finish?
I could use a setTimeout()
but I'd like a cleaner solution that doesn't depend on the speed of the network.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想最基本的解决方案(除了循环 setTimeout 之外)可能是使用类似的方法而不是 save() 函数
,然后在 iframe onload 上使用类似的方法
I guess the most basic solution (except for looping setTimeout) could be to have something like this instead of save() function
and then on that iframe onload to have something like
您可以使用jquery $.ajax()
您必须拦截第一次提交的点击事件,通过ajax发送表单数据,然后您可以绑定到ajax插件完成回调函数并使第二个iframe提交。
希望这有帮助。
You could use the jquery $.ajax()
You would have to intercept the click event of the first submit, send the form data via ajax and then you could tie into the ajax plugins complete callback function and make the second iframe submit.
Hope this helps.