[英]How do I add multiple values to a textarea in Javascript?
I'm new to Javascript and working on a problem, it's about making a website that allows a user to input a bid on an item along with a bid id.我是 Javascript 的新手,正在解决一个问题,它是关于制作一个网站,允许用户输入对项目的出价和出价 ID。 When they input both they press the submit button and the bid/bid id (along with the date and time) will display in a textarea.
当他们同时输入时,他们按下提交按钮,出价/出价 ID(连同日期和时间)将显示在文本区域中。
It should allow for multiple bids to be submitted and to display but currently I can only get one to display.它应该允许提交和显示多个出价,但目前我只能显示一个。 Any help as to how I can get multiple bids to display would be appreciated.
任何有关如何获得多个出价显示的帮助将不胜感激。 Thank you
谢谢
var bids = new Array();
var bidders = new Array();
var bidTime = new Array();
function writeBid() {
var historyText = " ";
for (var i = 0; i < bids.length; i++) {
historyText = bidTime[i] + " " + bids[i] + " " + bidders[i] + "\n";
document.bidForm.bidList.value = historyText;
document.bidForm.highBid.value = bids[i];
document.bidForm.bidId.value = " ";
document.bidForm.bidAmount.value = " ";
}
}
function addBid() {
bidders.unshift(document.bidForm.bidId.value);
bids.unshift(document.bidForm.bidAmount.value);
var now = new Date();
var hours = now.getHours();
var minutes = now.getMinutes();
var seconds = now.getSeconds();
var timeText = hours + ":" + minutes + ":" + seconds;
bidTime.unshift(timeText);
writeBid();
}
function removeBid() {
bids.shift();
bidders.shift();
bidTime.shift();
writeBid();
}
as @nnnnnn said using +=
with your text variable works perfect:正如@nnnnnn 所说,将
+=
与您的文本变量一起使用非常完美:
JavaScript JavaScript
var bids = [10, 20, 30];
var bidders = ['tim', 'sam', 'john'];
var bidTime = ['10/2/2013','12/5/213','14/1/2023'];
function writeBid() {
var historyText = " ";
for (var i = 0; i < bids.length; i++) {
historyText += bidTime[i] + " " + bids[i] + " " + bidders[i] + "\n";
document.bidForm.highBid.value = bids[i];
document.bidForm.bidId.value = " ";
document.bidForm.bidAmount.value = " ";
}
document.bidForm.bidList.value = historyText;
}
HTML HTML
<form name="bidForm" id="bidForm">
<input type="text" name="bidId" id="bidId"/>
<input type="text" name="bidAmount" id="bidAmount"/>
<input type="text" name="highBid" id="highBid"/>
<textarea name="bidList" id="bidList"></textarea>
</form>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.