Ajax textarea保存错误

时间:2012-08-23 18:39:55

标签: javascript json jquery

我尝试做ajax编辑textarea。当我点击“编辑”按钮时,我会收到textarea文本进行编辑,当保存文本区域消失并且编辑后的文本全部必须在

中显示
    jQuery('.photocont #commeditCont').animate({opacity: 0}, timer, function()
    {
        if (id == 'commedit') jQuery(this).html('<textarea>'+jQuery(this).html().replace(/<br>/gi,"\n")+'</textarea>');
        else if (id == 'commsave') {

            var link = window.location.protocol + '//' + window.location.host + 'picture_edit';

            jQuery.ajax({
                type: "POST",
                url: link,
                data: { image : image, description : jQuery(this).html(jQuery(this).find('textarea').val()) },
                dataType: "json",
                context: this,
                success: function(data) {
                    jQuery(this).html(jQuery(this).find('textarea').val().replace(/\n/g,"<br>"));
                },
                error:function (xhr){
                    if (xhr.status == 401) {
                        window.location.href = link;
                    }
                }
            });

        }
});

在ajax success: function我尝试从textarea获取文本并替换为new my div。不幸的是我收到了错误

NS_ERROR_XPC_BAD_CONVERT_JS: Could not convert JavaScript argument

...a[2]||k.error(a[0]),a[2]=a[2].replace(/^\+|\s*/g,"");var b=/(-?)(\d*)(?:n([+\-]?...

jquery....min.js (line 16)

TypeError: jQuery(this).find("textarea").val() is undefined 

jQuery(this).html(jQuery(this).find('textarea').val().replace(/\n/g,"<br>"));

错误来自控制台。

2 个答案:

答案 0 :(得分:0)

this超出了成功函数的范围,因此未定义。尝试缓存它并使用它,如下所示:

jQuery('.photocont #commeditCont').animate({opacity: 0}, timer, function() {
    if ( id == 'commedit' ) {
        jQuery(this).html('<textarea>' + jQuery(this).html().replace(/<br>/gi, "\n") + '</textarea>');
    }
    else if ( id == 'commsave' ) {

        var link = window.location.protocol + '//' + window.location.host + 'picture_edit',
            self = this;

        jQuery.ajax({
            type    : "POST",
            url     : link,
            data    : { image: image, description: jQuery(this).html(jQuery(this).find('textarea').val()) },
            dataType: "json",
            context : this,
            success : function( data ) {
                jQuery(self).html(jQuery(self).find('textarea').val().replace(/\n/g, "<br>"));
            },
            error   : function( xhr ) {
                if ( xhr.status == 401 ) {
                    window.location.href = link;
                }
            }
        });

    }
});

答案 1 :(得分:0)

看看你在这做什么

description: jQuery(this).html(jQuery(this).find('textarea').val())

这是您要发送给服务器的吗?我想你想要文本

真正的问题是你的回调在错误的范围

           success: function(data) {
                jQuery(this).html(jQuery(this).find('textarea').val().replace(/\n/g,"<br>"));
            },

如果你不能一遍又一遍地使用jQuery(this),那就没有问题了。你需要保持范围。

jQuery('.photocont #commeditCont').animate({
    opacity: 0
}, timer, function () {
    var div = jQuery(this);
    if (id == 'commedit') div.html('<textarea>' + div.html().replace(/<br>/gi, "\n") + '</textarea>');
    else if (id == 'commsave') {

        var link = window.location.protocol + '//' + window.location.host + 'picture_edit';

        jQuery.ajax({
            type: "POST",
            url: link,
            data: {
                image: image,
                description: div.html(jQuery(this).find('textarea').val()) //I still think this is not right
            },
            dataType: "json",
            context: this,
            success: function (data) {
                div.html(div.find('textarea').val().replace(/\n/g, "<br>"));
            },
            error: function (xhr) {
                if (xhr.status == 401) {
                    window.location.href = link;
                }
            }
        });

    }
});