向文本区域添加多个值

时间:2014-10-22 15:56:21

标签: javascript html forms textarea invoice

我正在创建发票,并且我试图让多个值进入我创建的文本区域。

textarea看起来像这样

<textarea input type ="text" name = "textarea" id = "textarea" rows = "12" cols = "180" placeholder = "--Item Code--  --Item Name--   --Item Cost-- --Quantity--  --Subtotal--  --Tax--  --Total--"></textarea>

我还有一个名为

的按钮
<div id="AddItemButton">
            <td align = "left"><input type="button" value= "Add Item" id = "add" onclick="computeCost();"/>
            </td> 
        </div>

我将我想要进入textarea 'document.getElementById'

的值连接起来
document.getElementById('textarea').value = ("\n")+("\n") + "--" + code + "--" + " " + " " + "--Item Name--" + "--" +  itemName + "--" + " " + " " + "--" + cost +"--" + " " + " " + "--" + quantity + "--" + " " + " " + "--" + subtotal + "--" + " " + " " + " " + "--" + tax + "--" + " " + " " + "--" + total + "--";

总计,小计,税金等都显示在文本区域中。我遇到的问题是,每当我点击&#34;添加项目&#34;它取代了旧的价值观。我希望将新值添加到文本区域,同时保存旧值。

1 个答案:

答案 0 :(得分:4)

然后在插入值时使用+=

document.getElementById('textarea').value += ("\n")+("\n") + "--" + code + "--" + " " + " " + "--Item Name--" + "--" +  itemName + "--" + " " + " " + "--" + cost +"--" + " " + " " + "--" + quantity + "--" + " " + " " + "--" + subtotal + "--" + " " + " " + " " + "--" + tax + "--" + " " + " " + "--" + total + "--";

它将获取当前value - 并将新项目添加到其中。