表单未提交到 iFrame,在新选项卡中打开

发布于 2024-12-03 02:32:10 字数 1155 浏览 0 评论 0原文

我正在使用以下 html 开头表单代码:

<form id="uploadpic" method="post" target="uppic" name="upform" action="/cgi-bin/inpost.cgi" enctype="multipart/form-data">

尝试加载页面,但是页面会在新选项卡中打开。

我不认为这是问题的一部分,但无论如何我都会将其包括在内。这是表单中的提交按钮代码:

<input name="filename" id="filename" type="file" onchange="submitFormAfterImageCheck();" /> 

这是它调用的函数:

function submitFormAfterImageCheck()
            {
                if(/(\.jpeg|\.jpg|\.JPG|\.gif|\.png|\.tiff)$/.test(document.getElementById("filename").value))
                {
                    nogo = "go";
                    document.getElementById("uploadpic").submit();
                    $("#upload").html("<center>LOADING...</center>");
                    $('#link').hide();
                    $('#update_post').hide();

                }
                else
                {
                    alert("You can only upload an image.");
                }
            }

iFrame 代码:

<iframe id="uppic" name="uppic"></iframe>

为什么这不是提交到 iframe?

I'm using the following html beginning form code:

<form id="uploadpic" method="post" target="uppic" name="upform" action="/cgi-bin/inpost.cgi" enctype="multipart/form-data">

to try and load a page, however the page opens in a new tab.

I don't think this is part of the problem, but I'll include this anyways. This is the submit button code in the form:

<input name="filename" id="filename" type="file" onchange="submitFormAfterImageCheck();" /> 

And this is the function it calls:

function submitFormAfterImageCheck()
            {
                if(/(\.jpeg|\.jpg|\.JPG|\.gif|\.png|\.tiff)$/.test(document.getElementById("filename").value))
                {
                    nogo = "go";
                    document.getElementById("uploadpic").submit();
                    $("#upload").html("<center>LOADING...</center>");
                    $('#link').hide();
                    $('#update_post').hide();

                }
                else
                {
                    alert("You can only upload an image.");
                }
            }

iFrame code:

<iframe id="uppic" name="uppic"></iframe>

Why isn't this submitting to the iframe?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

和我恋爱吧 2024-12-10 02:32:10

我也遇到了同样的问题并找到了解决方法。

当您静态设置表单的 target 属性时(正如您所做的那样),即使目标 iframe 位于同一窗口中,也会在新选项卡中收到响应/窗户。

您可以通过使用 javascript/jQuery 动态设置 formtarget 属性来覆盖此设置。

javascript:

document.getElementById(formId).target = iframeId;

jQuery:

$("#formId").attr('target', iframeId);

希望这有帮助。

I also faced the same problem and found a work around.

When you set the target attribute of a form statically (as you are doing), even if the target iframe is in the same window, response is received in a new tab/window.

You can override this by setting the target attribute of form dynamically using javascript/jQuery.

javascript:

document.getElementById(formId).target = iframeId;

jQuery:

$("#formId").attr('target', iframeId);

Hope this helps.

小ぇ时光︴ 2024-12-10 02:32:10

如果您的 DOM 元素没有问题(如 @johndoe 在他的回答中指出的),并且在使用 Javascript 设置目标时仍然遇到问题(如 @naren 在他的回答中指出的),那么这应该可以解决问题

: iFrame 名称属性并将表单目标设置为该值:

<iframe name="myTarget"></iframe>

<form target="myTarget" method="post" ...></form>

If you are not having issues with your DOM elements (as @johndoe noted in his answer) and you are still having the problem when setting the target using Javascript (as @naren noted in his answer) then this should solve the problem:

Give your iFrame a name attribute and set your form target to that value:

<iframe name="myTarget"></iframe>

<form target="myTarget" method="post" ...></form>
傲鸠 2024-12-10 02:32:10

DIV 元素的放置似乎是问题所在。虽然它被放置在表单标签内,但一旦表单提交,它就不会重定向。当从表单标签中删除时,它可以很好地重定向。

The placement of the DIV element seemed to be the problem. While it was placed within the form tags, it never redirected once the form submitted. When removed from the form tags, it redirected fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文