动态地将textarea数据添加到页面

时间:2013-10-24 14:58:29

标签: javascript jquery

我有以下jQuery代码,它可以获取一组文本框中的所有文本,然后在页面上的其他位置显示新<h2></h2>中“<div>”标记之间的文本。

代码确实有效,但由于某种原因,它会在最后添加一个空的<h2></h2>

因此,例如,如果页面上有3个文本框,它将显示这3个文本框,但随后将空<h2></h2>添加到最后,如下所示:

   <div id="desc1">
       <h2>description 1</h2>
       <h2>description 2</h2>
       <h2>description 3</h2>
       <h2></h2>    <!--why is this here?-->
   </div>

我无法弄清楚发生了什么。有人可以提出任何建议吗?

var newDiv = $("<div>").attr("id", 'desc' + counter);

var descriptions;
$(function () {
       descriptions = $('textarea.descriptionTxt').map(function () {
            return "<h2>" + this.value + "</h2>";
       }).get();
       $(newDiv ).html(answers.join(''));
});

$(newDiv).insertAfter(titleDiv).html();

谢谢!

1 个答案:

答案 0 :(得分:3)

也许你最后的描述是空的?尝试:

   descriptions = $('textarea.descriptionTxt[value!=""]').map(function () {
        return "<h2>" + this.value + "</h2>";
   }).get();
相关问题