使用“display:none”时使用 jQuery 捕获表单提交时出现问题

发布于 2024-08-22 11:07:46 字数 4472 浏览 9 评论 0原文

我正在尝试使用 jQupload 异步上传图像。该脚本使用页面外的 iframe 进行上传,然后捕获 iframe 的 .load() 事件以返回返回的 JSON 消息。

如果我在标准 HTML 页面的底部显示表单并包含 javascript,那么它就可以正常工作。但是,我想将上传表单加载到模态窗口中,并且为此使用 Facebox。因此,该表单包含在页面底部设置为“display:none”的样式标签中,并且当单击特定按钮时,它会显示在模式窗口中。

在模态窗口中,表单提交没有问题并且 iframe 已填充,但是 javascript 没有捕获表单提交事件的代码。我尝试在函数中警告一个简单的字符串,但没有得到结果。但是,如果我使用表单的“onsubmit”标签警告出一个字符串,那么它就可以正常工作。

以下代码行执行正常并向表单添加目标属性:

$(this).attr("target",data.iframe);

这是不在模态窗口内触发的下一行代码:

$(this).submit(function(){
                            alert('hello!');
                $("#"+data.iframe).load(function(){
                    var data1=$("#"+data.iframe).contents().find("body").html();
                    data1='{"formid":{"value":"'+id+'"},'+data1.substr(1);
                    if($.jQupload.callback[id]){
                        eval($.jQupload.callback[id]+"('"+data1+"')")
                    }
                    else{
                        if($.jQupload.output[id]){
                            $.jQupload.jsonMessage(data1)
                        }
                        else{
                            $.jQupload.defaultMessage(data1)
                        }
                    }
                    $("#"+data.iframe).contents().find("body").html("");
                    $("#"+data.iframe).unbind("load")
                })
            });

此代码来自使用以下 2 行调用的 jQupload 函数内部

$("#image_upload_form").jqupload();
$("#image_upload_form").jqupload_form();

:是完整的 jQupload 代码,并且按照 http://jqframework.com/jqupload_doc.html

    /*  jQupload - jQuery Upload v0.1
 *  jQupload is distributed under the terms of the MIT license
 *  For more information visit http://jqframework.com/jqupload
 *  Copyright (C) 2010  jqframework.com
 * Do not remove this copyright message
 */
$.jQupload = {
    fadeOutTime:3000,
    callback:{},
    output:{},
    init: function(id,obj){
        if(obj.callback){
            $.jQupload.callback[id]=obj.callback
        }
        if(obj.output){
            $.jQupload.output[id]=obj.output
        }
        if(obj.fadeOutTime){
            $.jQupload.fadeOutTime=obj.fadeOutTime
        }
    },
    defaultMessage:function(data){
        alert(data)
    },
    jsonMessage:function(data){
        eval("data="+data);
        $("#"+$.jQupload.output[data.formid.value]).html(data.message).fadeOut($.jQupload.fadeOutTime)
    }
};

$.fn.extend({
    jqupload:function(obj){
        return this.each(function(){
            var id=this.id;
            if(typeof this.id=="object"){
                id=$(this).attr("id")
            }
            if(!obj)
                obj={};
            $.jQupload.init(id,obj)
        })
    },
    jqupload_form:function(){
        return this.each(function(){
            var id=this.id;
            if(typeof this.id=="object"){
                id=$(this).attr("id")
            }
            var data=$.extend(
                {},
                {iframe:id+"_iframe"},
                data
            );
            $("body").append("<iframe name="+data.iframe+' id="'+data.iframe+'"></iframe>');
            //$("#"+data.iframe).css({position:"absolute",left:"-1000px",top:"-1000px",width:"0px",height:"0px"});
            $("#"+data.iframe).css({position:"absolute",left:"0px",top:"0px",width:"500px",height:"500px"});
            $(this).attr("target",data.iframe);

            $(this).submit(function(){
                $("#"+data.iframe).load(function(){
                    var data1=$("#"+data.iframe).contents().find("body").html();
                    data1='{"formid":{"value":"'+id+'"},'+data1.substr(1);
                    if($.jQupload.callback[id]){
                        eval($.jQupload.callback[id]+"('"+data1+"')")
                    }
                    else{
                        if($.jQupload.output[id]){
                        }
                            $.jQupload.jsonMessage(data1)
                        else{
                            $.jQupload.defaultMessage(data1)
                        }
                    }
                    $("#"+data.iframe).contents().find("body").html("");
                    $("#"+data.iframe).unbind("load")
                })
            });
            return true
        })  
    }
});

有什么想法吗?

I am trying to use jQupload to upload an image asynchronously. The script uses an iframe off of the page to upload and then catches the .load() event of the iframe to return the JSON message that is returned.

If I display the form at the bottom of a standard HTML page and include the javascript then it works fine. However, I would like to load the upload form into a modal window and I am using facebox for this. The form is therefore included with the style tag set to "display:none" at the bottom of the page and it displays in the modal window when a specific button is clicked.

Within the modal window, the form submit's without a problem and the iframe is populated, however the code the form's submit event is not being caught by the javascript. I have tried alerting a simple string in the function but I get no result. However, if I alert out a string using the "onsubmit" tag of the form then it works ok.

The following line of code executes ok and adds a target attr to the form:

$(this).attr("target",data.iframe);

This is the next line of code that is not firing within the modal window:

$(this).submit(function(){
                            alert('hello!');
                $("#"+data.iframe).load(function(){
                    var data1=$("#"+data.iframe).contents().find("body").html();
                    data1='{"formid":{"value":"'+id+'"},'+data1.substr(1);
                    if($.jQupload.callback[id]){
                        eval($.jQupload.callback[id]+"('"+data1+"')")
                    }
                    else{
                        if($.jQupload.output[id]){
                            $.jQupload.jsonMessage(data1)
                        }
                        else{
                            $.jQupload.defaultMessage(data1)
                        }
                    }
                    $("#"+data.iframe).contents().find("body").html("");
                    $("#"+data.iframe).unbind("load")
                })
            });

This code comes from within the jQupload functions which are called using the following 2 lines:

$("#image_upload_form").jqupload();
$("#image_upload_form").jqupload_form();

This is the full jQupload code and is being used as described at http://jqframework.com/jqupload_doc.html :

    /*  jQupload - jQuery Upload v0.1
 *  jQupload is distributed under the terms of the MIT license
 *  For more information visit http://jqframework.com/jqupload
 *  Copyright (C) 2010  jqframework.com
 * Do not remove this copyright message
 */
$.jQupload = {
    fadeOutTime:3000,
    callback:{},
    output:{},
    init: function(id,obj){
        if(obj.callback){
            $.jQupload.callback[id]=obj.callback
        }
        if(obj.output){
            $.jQupload.output[id]=obj.output
        }
        if(obj.fadeOutTime){
            $.jQupload.fadeOutTime=obj.fadeOutTime
        }
    },
    defaultMessage:function(data){
        alert(data)
    },
    jsonMessage:function(data){
        eval("data="+data);
        $("#"+$.jQupload.output[data.formid.value]).html(data.message).fadeOut($.jQupload.fadeOutTime)
    }
};

$.fn.extend({
    jqupload:function(obj){
        return this.each(function(){
            var id=this.id;
            if(typeof this.id=="object"){
                id=$(this).attr("id")
            }
            if(!obj)
                obj={};
            $.jQupload.init(id,obj)
        })
    },
    jqupload_form:function(){
        return this.each(function(){
            var id=this.id;
            if(typeof this.id=="object"){
                id=$(this).attr("id")
            }
            var data=$.extend(
                {},
                {iframe:id+"_iframe"},
                data
            );
            $("body").append("<iframe name="+data.iframe+' id="'+data.iframe+'"></iframe>');
            //$("#"+data.iframe).css({position:"absolute",left:"-1000px",top:"-1000px",width:"0px",height:"0px"});
            $("#"+data.iframe).css({position:"absolute",left:"0px",top:"0px",width:"500px",height:"500px"});
            $(this).attr("target",data.iframe);

            $(this).submit(function(){
                $("#"+data.iframe).load(function(){
                    var data1=$("#"+data.iframe).contents().find("body").html();
                    data1='{"formid":{"value":"'+id+'"},'+data1.substr(1);
                    if($.jQupload.callback[id]){
                        eval($.jQupload.callback[id]+"('"+data1+"')")
                    }
                    else{
                        if($.jQupload.output[id]){
                        }
                            $.jQupload.jsonMessage(data1)
                        else{
                            $.jQupload.defaultMessage(data1)
                        }
                    }
                    $("#"+data.iframe).contents().find("body").html("");
                    $("#"+data.iframe).unbind("load")
                })
            });
            return true
        })  
    }
});

Any ideas?

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

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

发布评论

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

评论(1

执手闯天涯 2024-08-29 11:07:46

马特,我相信问题是您使用的选择器实际上并未将任何内容装配到表单中:

$(this).submit(function() {...

仅在特定上下文中有效,应该在任何地方都有效:

$("#myFormId").submit(function() {....

Matt, I believe the problem is the selector you're using not actually rigging anything up to the form:

$(this).submit(function() {...

only works in a certain context, it should be this to work everywhere:

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