【问题标题】:HTML textarea value is not getting from js in running timeHTML textarea 值在运行时未从 js 获取
【发布时间】:2020-05-28 07:43:13
【问题描述】:

我没有太多使用 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) 
        });

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

【问题讨论】:

  • 最后一行 $ 在这里似乎不完整....(没有右括号)
  • textarea 元素需要.html.text 我认为。但正如@Pierre 提到的,脚本无论如何都会失败
  • 这一行需要关闭标签 => $('#pg_social form#wall_post
  • @GetSet 不正确,将val() 与文本区域一起使用
  • 我不知道@RoryMcCrossan ...虽然有道理。为你点赞

标签: javascript jquery append


【解决方案1】:

你做得不对。

例如:

// 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 insert html in variable).
    var content = $('#wall_post_text').val();
    
    // insert value in div
    $('.content').html(content);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!-- 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 ... <-
});

【讨论】:

  • 大家好,感谢您的回复,这是正确的,当我复制/粘贴时我丢失了一些代码,我的问题是关于附加功能。
  • 其实,我的问题是关于如何使用附加功能。我正在尝试将一些 内容插入到 中。谢谢
  • 这是不可能的。您不能将 HTML 放在 textarea 中
  • 无法在 textarea 中插入 span!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-10-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-23
  • 1970-01-01
相关资源
最近更新 更多