【问题标题】:Unable to get value of the property 'submit': object is null or undefined无法获取属性“提交”的值:对象为空或未定义
【发布时间】:2013-02-07 09:51:42
【问题描述】:

我有一个像这样的 iframe 和表单:

<iframe id="FileUploadFrame" name="FileUploadFrame" src="" style="display: none; border: 0; width: 0; height: 0;">
            <form id="FileUploadForm" name="FileUploadForm" accept-charset="utf-8"  target="FileUploadFrame" enctype="multipart/form-data" encoding="multipart/form-data" method="POST" action="CIMtrek_Regional_WhseForm_FileUpload">
   </form>
</iframe>

这就是我提交iframe中的表单的方式

function fileUpload() {

    //var UploadForm = document.forms["FileUploadForm"];
    var UploadForm = global.forms["FileUploadForm"];

    alert("Form : "+typeof global.forms["FileUploadForm"]);
    alert("Form : "+typeof global.forms["FileUploadFrame"]);
    iframeId = document.getElementById("FileUploadFrame");


    // Add event...
    var eventHandler = function () {

        if (iframeId.detachEvent) iframeId.detachEvent("onload", eventHandler);
        else iframeId.removeEventListener("load", eventHandler, false);

        // Message from server...
        if (iframeId.contentDocument) {
            content = iframeId.contentDocument.body.innerHTML;
        } else if (iframeId.contentWindow) {
            content = iframeId.contentWindow.document.body.innerHTML;
        } else if (iframeId.document) {
            content = iframeId.document.body.innerHTML;
        }

        document.getElementById(div_id).innerHTML = content;
    }
    if (iframeId.addEventListener) iframeId.addEventListener("load", eventHandler, true);
    if (iframeId.attachEvent) iframeId.attachEvent("onload", eventHandler);

    //alert('submitting form');
    //form.submit();
    //document.forms["FileUploadForm"].submit();
    alert(UploadForm.submit());
}

但我在提交表单时遇到以下异常:

Error: Unable to get value of the property 'submit': object is null or undefined

请帮我找出问题并解决。

最好的问候

【问题讨论】:

  • 什么是 global.forms ?它有什么作用 ?全局是一个jsp还是另一个html标记在另一个文件中?
  • var global = window.document,我也试过下面的var UploadForm = document.forms["FileUploadForm"]var UploadForm = document.forms["FileUploadForm"].submit()
  • 你把javascript代码放在哪里了?在 iframe 内部还是外部?
  • @Anto 我建议用console.log代替alert,先试试看global是否设置为anyting:在IE中console.log("global is now:",global)就可以开始了在脚本下按F12调试就可以开始调试了。完成后,IE 对 console.log 的支持(很差)
  • 我建议你通过这个:stackoverflow.com/questions/14353748/… 这处理这种 iframe 特定的问题。通常问题是,它永远找不到 iframe,因为 id 在父级中,因此您需要 gove parent.document.getElementByID()。您也可以使用 frameElement 直接获取 iframe-id,然后您可以将其提供给 document.getElementbyD(frameElementID)--> 这样会更好,并且总是会获取 frameElement。谷歌看看如何使用 FrameElement 。

标签: javascript html forms submit


【解决方案1】:

FileUploadForm的获取方式是:

iframeId = document.getElementById("FileUploadFrame");
//This is the trick
var ifrDoc = iframeId.contentDocument || iframeId.contentWindow.document;
var UploadForm = ifrDoc.getElementById("FileUploadForm");

或者您可以在框架中有一些代码将父级中的变量设置为表单,但我不相信这种方法。

来源:accessing a form that is in an iframe


编辑:

此外,为了使上传表单有效,它需要看起来像任何其他表单一样,除了:

  • form标签必须指定POST方法
  • 表单标签必须指定multipart/form-data的enctype
  • 表单必须包含&lt;input type=file&gt; 元素。

【讨论】:

  • 上一个已经不见了,现在就得到这个Error: TypeError: UploadForm is null
  • @Anto:检查更新,表单内至少需要&lt;input type=file&gt;元素
  • 我在父表单中有文件输入,我必须通过这个 iframe 保存在父表单中选择的文件,我应该像 ajax 调用但实际上不是。在这种情况下,如何获取在父表单中选择的文件并将文件分配给 iframe 表单中的文件输入
【解决方案2】:

没有document.forms["FileUploadForm"],因为您的表单位于 iframe 中。 iframe 的document 与你的脚本执行的document 不同。

在你的脚本中,在函数的开头添加这一行

var iframe = document.getElementById('FileUploadFrame');
var innerDoc = iframe.contentDocument || iframe.contentWindow.document;
var UploadForm = innerDoc.forms["FileUploadForm"];

【讨论】:

    猜你喜欢
    • 2012-06-11
    • 2013-04-24
    • 1970-01-01
    • 2023-03-25
    • 2014-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多