附加 jquery 对象会在 IE 中引发无效参数

发布于 2024-08-05 01:27:27 字数 1051 浏览 4 评论 0原文

我正在尝试创建一个 jQuery 插件,将表单克隆到 iframe,然后提交表单。这是一个伪造 ajax 文件上传的 hack。我也知道已经有这样的插件了,但我没有一个能够满足我的需求。我在将 jQuery 对象附加到 IE 中的任何内容时遇到问题。 (我正在运行 IE 7.0)我已经尝试过 FF 并且它工作得很好。我还使用 jQuery 1.3.2 分钟。任何帮助将不胜感激。

(function($){

$.fn.extend({ 

//pass the options variable to the function
formUploader: function(options) {

    //Set the default values, use comma to separate the settings, example:
    var defaults = {
        onSubmit: function(){ return true;},
        onComplete: function(){ return true;}
    }

    var settings =  $.extend(defaults, options);

    return this.each(function() {
        $(this).submit(function(){
        var id = "asdfasda";
    $(document).find('body').append('<iframe src="javascript:false;" name="' + id + '" id="' + id + '" />');

    //this should be a form
    var curEl = this;
    setTimeout(function()
    {
        //fails in IE but not in FF
        $("#"+id).contents().find("body").append($(curEl).clone());
    },250);
    return false;
        });
    });

    }
});

})(jQuery);

I am trying to create a jQuery plugin that will clone a form to an iframe then submit a form. It is a hack to fake an ajax file upload. I also know there are plugins like this already, but none that I have trie meet my needs. I have a problem appending a jQuery object to anything in IE. (I am running IE 7.0) I have tried FF and it works perfectly. I am also using jQuery 1.3.2 min. Any help would be greatly appreciated.

(function($){

$.fn.extend({ 

//pass the options variable to the function
formUploader: function(options) {

    //Set the default values, use comma to separate the settings, example:
    var defaults = {
        onSubmit: function(){ return true;},
        onComplete: function(){ return true;}
    }

    var settings =  $.extend(defaults, options);

    return this.each(function() {
        $(this).submit(function(){
        var id = "asdfasda";
    $(document).find('body').append('<iframe src="javascript:false;" name="' + id + '" id="' + id + '" />');

    //this should be a form
    var curEl = this;
    setTimeout(function()
    {
        //fails in IE but not in FF
        $("#"+id).contents().find("body").append($(curEl).clone());
    },250);
    return false;
        });
    });

    }
});

})(jQuery);

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

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

发布评论

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

评论(2

○愚か者の日 2024-08-12 01:27:27

将表单克隆到 iframe,然后提交表单

从示例中很难看出,但如果您尝试将元素克隆到不同的文档中,则 DOM 标准不允许这样做。一个文档创建的节点不能插入到另一文档中。 iframe 的内容是不同的文档。

这样做的方法应该是使用 document.importNode()

var iframe= document.getElementById('myiframe');
var idoc= iframe.contentDocument || iframe.contentWindow.document;
var form= document.getElementById('myform');
idoc.body.appendChild(idoc.importNode(form, true));

但是 IE 根本不支持 importNode。不过,IE 已经支持 XMLHttpRequest,因此这可能不是问题。您要为哪些不支持 XMLHttpRequest 的浏览器实现此功能?小心:旧的/损坏的浏览器也可能不支持 importNode,甚至 jQuery!

请注意,如果您想使用 jQuery 的便捷方法在 iframe 文档中创建节点,则必须有由

var inp= idoc.$('<input type="hidden" name="thing" />');
inp.val(thingvalue);
idoc.$('#myform').append(inp);
...

clone a form to an iframe then submit a form

Difficult to see from the example, but if you are trying to clone an element into a different document, that's not allowed by the DOM standard. Nodes created by one document cannot be inserted into another document. The contents of an iframe are a different document.

The way to do it should be to use document.importNode():

var iframe= document.getElementById('myiframe');
var idoc= iframe.contentDocument || iframe.contentWindow.document;
var form= document.getElementById('myform');
idoc.body.appendChild(idoc.importNode(form, true));

But IE doesn't support importNode at all. Then again, IE already supports XMLHttpRequest, so that may not be an issue. What browsers are you implementing this for, that don't support XMLHttpRequest? Careful: browsers that old/broken may also not support importNode, or even jQuery!

Note that if you want to use jQuery's convenience methods to create nodes in the iframe's document, you must have a second copy of jQuery loaded by a <script> tag in the iframe, which you call via the other document's copy of $. Each jQuery instance is tied to the document that included it; a jQuery wrapper from one document cannot be passed to another document.

var inp= idoc.$('<input type="hidden" name="thing" />');
inp.val(thingvalue);
idoc.$('#myform').append(inp);
...
南城旧梦 2024-08-12 01:27:27

您是否研究过 jQuery Form 插件,它允许您使用 iframe 提交文件输入字段,检查也许

您可以重复使用相同的内容,而不是重新发明轮子。

Did you look into the jQuery Form plugin, it lets you submit file input field using an iframe, check it here

Perhaps you can re-use the same rather than re-inventing the wheel.

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