如何使用按钮将表格中的文本添加到textarea输入?

时间:2015-04-01 15:43:59

标签: javascript jquery html

当我按下"添加"时,如何从表中显示文本以显示在textarea框中?按钮?

我不需要在添加数据之后将数据消失,我只想要"将此文本添加到textarea"出现在textarea框中。

https://jsfiddle.net/bj1sh4cq/

HTML

<div class="table-responsive">
    <table class="table table-hover">
        <tr>
            <td><button class="btn btn-success" id="addButton">ADD</a>

            </td>
            <td><span id="addText"><strong>Add this text to textarea.</span>

                </strong>
            </td>
        </tr>
        <tr>
            <td><button class="btn btn-success" id="addButton">ADD</a>

            </td>
            <td><span id="addText"><strong>Add this text to textarea.</span>

                </strong>
            </td>
        </tr>
        <tr>
            <td><button class="btn btn-success" id="addButton">ADD</a>

            </td>
            <td><span id="addText"><strong>Add this text to textarea.</span>

                </strong>
            </td>
        </tr>
        <tr>
            <td><button class="btn btn-success" id="addButton">ADD</a>

            </td>
            <td><span id="addText"><strong>Add this text to textarea.</span>

                </strong>
            </td>
        </tr>
        <tr>
            <td><button class="btn btn-success" id="addButton">ADD</a>

            </td>
            <td><span id="addText"><strong>Add this text to textarea.</span>

                </strong>
            </td>
        </tr>
        <tr>
            <td><button class="btn btn-success" id="addButton">ADD</a>

            </td>
            <td><span id="addText"><strong>Add this text to textarea.</span>

                </strong>
            </td>
        </tr>
        <tr>
            <td><button class="btn btn-success" id="addButton">ADD</a>

            </td>
            <td><span id="addText"><strong>Add this text to textarea.</span>

                </strong>
            </td>
        </tr>
        <tr>
            <td><button class="btn btn-success" id="addButton">ADD</a>

            </td>
            <td><span id="addText"><strong>Add this text to textarea.</span>

                </strong>
            </td>
        </tr>
    </table>
</div>
</div>
</div>
<br>
<div class="form-group">
    <textarea type="text" id="reviewText" placeholder="See Text Here." class="form-control" columns="10" rows="10"></textarea>
</div>
</div>
</div>

JS

 $('#addButton').on('click', function (){             

         var insertText = $('#addText').val();
         $('#reviewText').val($('#reviewText').val() + " "+insertText);
});

1 个答案:

答案 0 :(得分:1)

首先,您无法在页面上定义多个ID。其次你应该改变:

var insertText = $('#addText').val(); 

为:

$('#reviewText').val($(this).closest('td').next().text());

以下是更新后的fiddle