0

奇怪的是,我找不到这个问题的答案,但是,我收到了这个错误:

TypeError: 'undefined' is not a function (evaluating 'i.replace(et,"<$1></$2>")')

它似乎来自这行代码(我不得不煞费苦心地注释每一行代码才能找到它):

$.post("include/ajax.php", {updateWorkArt:"1", title:title, height:height, width:width, medium:medium, type:type, id:id, left:fin, top:top}, function(data){},"json");

这是完整的代码:

$(document).on("click", "#updateWorkPopup", function(){
        var title = $("#updateWorkTitle").val();
        var height = $("#updateWorkHeight").val();
        var width = $("#updateWorkWidth").val();
        var medium = $("#updateWorkMedium").val();
        var type = $("#updateWorkAssetType").val();

        var id = $("#whiteBgPopup").attr("data-workid");

        if($.trim(title) != "" && $.trim(height) != "" && $.trim(width) != "" && $.trim(medium) != "" && $.trim(type) != "")
        {
            var left = parseInt($("#offsetContainer").position().left);

            var containerMid = parseInt($(window).width()/2);

            var fin = (-containerMid+left)*-1;

            var top = parseInt($("#wall").height()/2);

            $.post("include/ajax.php", {updateWorkArt:"1", title:title, height:height, width:width, medium:medium, type:type, id:id, left:fin, top:top}, function(data){
                if(data.success)
                {   
                    $("#popUp").fadeOut("slow", function(){
                        $("#popUp").find("input").each(function(){
                            $(this).val("").blur();
                        });

                        $("#wall").append("<div class='adjustArtwork' data-workid='" + id + "' style='left:" + fin + "px; top:" + top + "px;' ><img src='" + data.image + "' width='" + data.width + "' /></div>", function(){
                            alert("finished");
                        });
                    });
                }
                alert("yes!");
            },"json");
        }
    });

这个错误到底是什么意思?

4

2 回答 2

0

这意味着(很可能)您的变量i引用了一个没有名为replace.

当你要求

i.replace

JavaScript 查找 的值,i发现它是一个对象;然后查找它的replace属性并没有找到它,所以它返回了undefined

然后它尝试i.replace作为函数调用,但“未定义不是函数”。

It is also possible that the value of i.replace actually was the value undefined (though this is rare and added only for completeness).

In either case, the code

i.replace(et,"<$1></$2>")

only works if the value of i.replace is a function.

于 2013-06-19T02:18:59.897 回答
0

I found the problem.

It appears that .append() does not have a callback function and was causing an error because it is undefined.

Thankfully though, .append() is synchronous so my code will load in the order it is written; no function() is needed.

于 2013-06-20T13:33:26.700 回答