1

我正在尝试创建一个 jQuery 插件,它将表单克隆到 iframe 然后提交表单。伪造ajax文件上传是一种黑客行为。我也知道已经有这样的插件,但我没有尝试满足我的需求。我在将 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);
4

2 回答 2

1

将表单克隆到 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 的文档中创建节点,您必须在 iframe 中通过 <script> 标记加载第二个 jQuery 副本,您可以通过另一个文档的$. 每个 jQuery 实例都与包含它的文档相关联;一个文档中的 jQuery 包装器不能传递给另一个文档。

var inp= idoc.$('<input type="hidden" name="thing" />');
inp.val(thingvalue);
idoc.$('#myform').append(inp);
...
于 2009-09-16T10:50:32.830 回答
0

您是否查看过 jQuery Form 插件,它允许您使用 iframe 提交文件输入字段,请在此处查看

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

于 2009-11-25T13:40:30.693 回答