简体   繁体   English

删除之前附加的内容后,append()不起作用

[英]append() doesn't work after previously appended contents deleted

I convert user input urls to bbcode and append it to the textarea, but after you delete one of the lines I appended it won't append more(but you could see the newly appended values in the firebug,really strange).Here's my code: 我将用户输入的URL转换为bbcode并将其附加到textarea,但是在你删除其中一行之后它将不会附加更多(但你可以在firebug中看到新附加的值,真的很奇怪)。这是我的代码:

$(function(){
    $(".addUrl").click(function(){
        $("#addUrl").slideDown();
    })
    $('#su').click(function(){
        if($("#u").val().length>3)
        addUrl($("#u").val());
        $("#u").val("");
    })
    $("input[value=\"x\"]").click(function(){$("#addUrl").fadeOut();})
})
function addUrl(e)
{
    patt="http[s]*:\/\/";
    if(e.match(patt))
        u=e;
    else
        u="http://"+e;
    $("textarea[name=\"content\"]").append("\n\r[url]"+u+"[/url]\n\r");
}

And here's the jsfiddle: http://jsfiddle.net/FpSsc/ 这里是jsfiddle: http//jsfiddle.net/FpSsc/

It's because you are changing the html inside the textarea which is the default value. 这是因为您正在更改textarea中的html,这是默认值。 It seems that as soon as you have set a new value (by typing or deleting something from the textbox) this default value is ignored. 看来,只要您设置了新值(通过在文本框中键入或删除某些内容),就会忽略此默认值。

To get round this you need to set the value of the textarea rather than append to the content: 要绕过这个,您需要设置textarea的值而不是附加到内容:

function addUrl(e)
{
    patt="http[s]*:\/\/";
    if(e.match(patt))
        u=e;
    else
        u="http://"+e;

    var newVal = $("textarea[name=\"content\"]").val() + "\n\r[url]"+u+"[/url]\n\r"
    $("textarea[name=\"content\"]").val(newVal);
}

http://jsfiddle.net/infernalbadger/FpSsc/1/ http://jsfiddle.net/infernalbadger/FpSsc/1/

Or as Felix recommended: 或者像菲利克斯推荐的:

function addUrl(e)
{
    patt="http[s]*:\/\/";
    if(e.match(patt))
        u=e;
    else
        u="http://"+e;

    $("textarea[name=\"content\"]").val(function(i, v) { return v + "\n\r[url]"+u+"[/url]\n\r"; });
}

I may advice you to use val here 我建议你在这里使用val

function addUrl(e)
    {
        patt="http[s]*:\/\/";
        if(e.match(patt))
            u=e;
        else
            u="http://"+e;
        $("textarea[name=\"content\"]").val( $("textarea[name=\"content\"]").val() + "\n\r[url]"+u+"[/url]\n\r");
    }

please try this addUrl function. 请尝试这个addUrl函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM