表单提交到目标iframe只能运行一次 [英] Form submit to target iframe only works once

查看:217
本文介绍了表单提交到目标iframe只能运行一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常恼人的问题,我确信我以前做过的事情。设置如下:

I'm having a really irritating problem doing something that I'm sure I've done before. The setup is this:


  • 有一个表单通过按钮上的click处理程序提交,尽管提交最终很简单调用表单的原生 submit()函数。

  • 表单的目标是iframe,最初填充空页(也就是说,它的源对象是空页面的URL。也就是说,表单的target属性是相同的标识符(是的,它是一个有效的标识符)作为iframe的name和id属性(是的,它是唯一的)

  • There's a form which is submitted via a "click" handler on a button, though the submit is ultimately a simple call to the form's native submit() function.
  • The form's target is an iframe, which is initially filled with an empty page (that is, its source object is an URL for an empty page). That is, the "target" attribute of the form is the same identifier (and yes, it's a valid identifier) as the "name" and "id" attributes of the iframe (and yes, it's unique)

现在,此设置的目标如下:表单包含文件上载字段。如果上传的其中一个文件出现问题,服务器将报告错误。如果允许错误响应(即带有表单新副本的html页面以及相应的错误消息)重新加载原始窗口,则将清除文件输入字段。这不好。

Now the goal of this setup is as follows: the form contains file upload fields. Should something be wrong with one of the files uploaded, the server will report back an error. If the error response (that is, the html page with a new copy of the form, along with appropriate error messages) were to be allowed to reload the original window, then the file input fields would be cleared. That's not good.

因此表单提交给iframe,以便来自服务器的响应可以是一个小页面,知道它在隐藏的iframe中,并且知道将错误消息移动到表单。

Thus the form submits to the iframe, so that the response from the server can be a small page that knows it's in that hidden iframe, and knows to move the error messages up to the form.

还有一件事:表单本身也在iframe中,作为弹出模式对话框的一部分(如jQuery UI对话框) ,虽然略有不同;但同样的想法。

One more thing: the form itself is also in an iframe, as part of a popup modal dialog (like a jQuery UI dialog, only slightly different; same idea though).

设置工作正常 - 在第一次提交时。换句话说,如果我提供好的快乐文件上传,服务器将返回成功的响应,并正确关闭对话框。如果我发送一个虚假文件,服务器会回复一个错误页面,该页面可以正确地将其内容复制到表单页面。

The setup works fine - on the first submit. In other words, if I supply nice happy files to upload, the server ships back the successful response, and the dialog is closed correctly. If I send a bogus file, the server responds with an error page that correctly copies its stuff up to the form page.

然而,在第二次提交时(如果我修复了伪造文件输入字段),浏览器坚持将服务器响应发送到新的浏览器选项卡。据我所知,表单的目标仍然正确,iframename和id属性没有改变,我甚至确保更新隐藏的iframewindow.name和window.id 价值观。这些都没有帮助;我总是得到一个新的浏览器选项卡。

On the second submit, however (like, if I fix the bogus file input field), the browser insists on sending the server response to a new browser tab. As far as I can tell, the form's "target" remains correct, the iframe "name" and "id" attributes aren't changed, and I even make sure to update the hidden iframe "window.name" and "window.id" values. None of that is helping; I always get a new browser tab.

我正在尝试设置一个稍微简单的测试用例,看看我是否可以排除我的一些框架代码(这些东西)这是提交),虽然通过一些console.log()调用我认为那些东西是好的;它在网站上的所有其他对话框等方面当然可以。当/如果我创建一个更简单的版本,我会发布它。与此同时,如果你们中任何一个疯狂聪明的人都认识到这种情况并且知道让它发挥作用的技巧,我会非常感激。

I'm trying to set up a slightly simpler test case to see if I can rule out some of my framework code (the stuff that does the submit), though via a few console.log() calls I think that stuff is OK; it's certainly OK on all the other dialogs etc. in the site. When/if I create a simpler version I'll post it. In the meantime, if any of you insanely smart people recognize this situation and know of a trick to make it work, I'd be really thankful.

我看到同样的Firefox(3.6)和Chrome中的行为,所以它必须是我的问题,而不是浏览器的怪癖(好吧,至少我认为这是真的)。​​

I see the same behavior in both Firefox (3.6) and Chrome, so it's got to be my problem and not a browser quirk (well, at least that's what I think to be true).

推荐答案

不知怎的,我的次要目标iframe似乎正在失去它的名称属性,尽管我不知道这是怎么发生的。 < iframe> 元素本身仍然具有正确的名称,但内部的窗口对象却没有 - 即使代码正在尝试设置它。

Well somehow my secondary "target" iframe seems to be losing it's "name" attribute, though I don't know how or when that happens. The <iframe> element itself still has the correct name, but the "window" object inside does not - even though code is trying to set it.

我现在可以通过对目标表单进行提交代码检查,查找目标iframe,然后确保其contentWindow具有名称来解决这个奇怪现象。与iframe上的name属性匹配的值。如何/为什么我必须这样做,我不知道,而且我确定这是由于我做错了其他事情。

I am able to hack around the weirdness for now by having that "submit" code check for a targetted form, find the target iframe, and then make sure that its contentWindow has a "name" value that matches the "name" attribute on the iframe. How/why I have to do that, I don't know, and I'm sure it's due to something else I'm doing wrong.

这篇关于表单提交到目标iframe只能运行一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆