【问题标题】:Make html() include content typed into textarea使 html() 包含输入到 textarea 中的内容
【发布时间】:2015-01-04 11:33:18
【问题描述】:

我有这个结构的网页:

<div id="report_content">
    Some information
    <textarea name="personal"></textarea>

    <b>Other information</b>
    <textarea name="work"></textarea>
</div>

在textareas中写了一些文本后,我使用jquery来获取整个html。结果就是textareas是空的,好像里面什么都没写。

我猜是因为他们不接受 html,但我需要获取包含 textarea 内容的 html。

到目前为止,我发现的唯一解决方案是将 textarea 转换为 div,然后为它们分配 textarea 内容。

还有其他方法可以避免这种转换吗?

【问题讨论】:

  • 您忘记发布您尝试过的 jQuery。

标签: javascript jquery html dom textarea


【解决方案1】:

问题是.html() 不会得到值(这是人们在 textearea 中输入的内容将进入的内容)。您可以在获取完整的 html 之前将 innerHTML 设置为该值,如下所示...

JSFiddle

$('textarea').each(function () {
    $(this).html($(this).val());
});

var html = $("#report_content").html();
console.log(html);

或者...使用较少的 jquery 包装...

var html = $("#report_content").find("textarea").each(function () {
    this.innerHTML = this.value;
}).end().html();

console.log(html);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-29
    • 2011-11-01
    • 2011-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-11
    相关资源
    最近更新 更多