[HTML textarea值在运行时未从js获取

问题描述 投票:-3回答:1

我在使用JS方面没有太多的经验,但是当我向textarea写入内容时,我试图使用JS从textarea获取值,但是它无法使用append函数。请您指教:

在HTML中:

<textarea required="" name="wall_post_text" id="wall_post_text" placeholder="Write something...">  </textarea>

javascript代码:

$(document).on('click', 'li.tag_system_search_people', function() {
            var username = $(this).attr('data-people_username');
            var user = $(this).attr('data-people');
            var content = $('#wall_post_text').val();

            content = content.replace(/@(\w+)/ig,'');
            $('#wall_post_text').val(content);
            $('#wall_post_text').append(" <span data-people='"+user+"' data-people_tagged='"+username+"' class='people_tagged' contenteditable='false'>"+username+"</span> ");
alert($('#wall_post_text').val());
            $('#pg_social form#wall_post 
        });

警报正在打印,但附加功能中没有文本。

提前感谢。

javascript jquery append
1个回答
0
投票

您做错了。

例如:

// find elements
$('.elm_submit').on('click', function() {
    /*var username = $(this).attr('data-people_username');
    var user = $(this).attr('data-people');*/
    
    // its value (You can too insertt html in variable).
    var content = $('#wall_post_text').val();
    
    // insert value in div
    $('.content').html(content);
});
<!-- Value -->
<textarea required="" name="wall_post_text" id="wall_post_text" placeholder="Write something...">  </textarea>

<!-- Get element --> 
<div class="content">

</div>

<!-- Fire -->
<button class="elm_submit">
Click
</button>

和您的邮政编码...$('#pg_social form#wall_post ...

© www.soinside.com 2019 - 2024. All rights reserved.