单击提交或删除上次出价时出错,错误分别指向addBid()和removeBid()。我的变量在.js文件中是全局的,我很累,很沮丧,也无法看到问题。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Auction Log</title>
<script src="modernizr-1.5.js"></script>
<link href="ahstyles.css" rel="stylesheet" type="text/css" />
<script src="bids.js" type="text/javascript"></script>
</head>
<body>
<form name="bidForm" id="bidForm">
<header>
<img src="logo.jpg" alt="Schmitt Auction Haus" />
<nav class="horizontal">
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">Auctions</a></li>
<li><a href="#">Features</a></li>
<li><a href="#">Schedule</a></li>
<li><a href="#">Contacts</a></li>
</ul>
</nav>
</header>
<section id="intro">
<h1>Silent Auction</h1>
<h2>Tawney Farm</h2>
<table id="summarytable">
<tr>
<th>Lot #121</th>
<td>TurfTuff Lawn Tractor X24 (2015)</td>
</tr>
<tr>
<th>Latest Bid</th>
<td>
<input id="latestBid" name="latestBid" readonly="readonly" />
</td>
</tr>
<tr>
<th>Bidding Ends</th>
<td>15:00</td>
</tr>
</table>
<table id="newbidtable">
<tr>
<th colspan="2" id="newtitle">Enter New Bid</th>
</tr>
<tr>
<th>Bidder ID</th>
<td>
<input id="bidId" name="bidId" />
</td>
</tr>
<tr>
<th>Bid Amount</th>
<td>
<input id="bidAmount" name="bidAmount" />
</td>
</tr>
<tr>
<th id="buttons" colspan="2">
<input type="button" value="Submit" onclick="addBid()" />
<input type="button" value="Remove Last Bid" onclick="removeBid()" />
</th>
</tr>
</table>
</section>
<section id="bidHistory">
<h1>Bid History</h1>
<textarea id="bidList" name="bidList"></textarea>
</section>
<footer>
<address>
Schmitt AuctionHaus ·
3401 Rural Route 4 ·
Fenbrook, IN 38012
</address>
</footer>
</form>
</body>
</html>
然后是.js文件
var bids = [];
var bidders = [];
var bidTime = [];
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.latestBid.value = bidders[0];
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();
if (hours < 10) {
hours = "0" + hours;
}
if (minutes < 10) {
minutes = "0" + minutes;
}
if (seconds < 10) {
seconds = "0" + seconds;
}
var timeText = "[" + hours + ":" + minutes + ":" + seconds + "]";
bidTime.unshift(timeText);
writeBid();
}
function removeBid() {
bidders.shift();
bids.shift();
bidTime.shift();
writeBid();
}
答案 0 :(得分:1)
在您的函数writeBid
中,您有historyText += bidTime[i] + " " + bids[i]+ "(" +bidders[i]+ ")" + n\;
行n/
到底是什么?我猜您最后会尝试添加换行符,因为您没有定义变量n
。
将行更改为
historyText += bidTime[i] + " " + bids[i]+ "(" +bidders[i]+ ")\n";
并且提交至少应该有效。
我还想在writeBid
中想要更改行
document.bidForm.bidList.value += historyText;
到
document.bidForm.bidList.value = historyText;
var bids = [];
var bidders = [];
var bidTime = [];
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.latestBid.value = bidders[0];
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();
if (hours < 10) {
hours = "0" + hours;
}
if (minutes < 10) {
minutes = "0" + minutes;
}
if (seconds < 10) {
seconds = "0" + seconds;
}
var timeText = "[" + hours + ":" + minutes + ":" + seconds + "]";
bidTime.unshift(timeText);
writeBid();
}
function removeBid() {
console.log(bidders);
console.log(bids);
console.log(bidTime);
bidders.shift();
bids.shift();
bidTime.shift();
writeBid();
console.log(bidders);
console.log(bids);
console.log(bidTime);
}
&#13;
<form name="bidForm" id="bidForm">
<header>
<img src="logo.jpg" alt="Schmitt Auction Haus" />
<nav class="horizontal">
<ul>
<li><a href="#">Home</a>
</li>
<li><a href="#">Auctions</a>
</li>
<li><a href="#">Features</a>
</li>
<li><a href="#">Schedule</a>
</li>
<li><a href="#">Contacts</a>
</li>
</ul>
</nav>
</header>
<section id="intro">
<h1>Silent Auction</h1>
<h2>Tawney Farm</h2>
<table id="summarytable">
<tr>
<th>Lot #121</th>
<td>TurfTuff Lawn Tractor X24 (2015)</td>
</tr>
<tr>
<th>Latest Bid</th>
<td>
<input id="latestBid" name="latestBid" readonly="readonly" />
</td>
</tr>
<tr>
<th>Bidding Ends</th>
<td>15:00</td>
</tr>
</table>
<table id="newbidtable">
<tr>
<th colspan="2" id="newtitle">Enter New Bid</th>
</tr>
<tr>
<th>Bidder ID</th>
<td>
<input id="bidId" name="bidId" />
</td>
</tr>
<tr>
<th>Bid Amount</th>
<td>
<input id="bidAmount" name="bidAmount" />
</td>
</tr>
<tr>
<th id="buttons" colspan="2">
<input type="button" value="Submit" onclick="addBid()" />
<input type="button" value="Remove Last Bid" onclick="removeBid()" />
</th>
</tr>
</table>
</section>
<section id="bidHistory">
<h1>Bid History</h1>
<textarea id="bidList" name="bidList"></textarea>
</section>
<footer> <address>
Schmitt AuctionHaus ·
3401 Rural Route 4 ·
Fenbrook, IN 38012
</address>
</footer>
</form>
&#13;
答案 1 :(得分:0)
在函数加载之前设置onclick属性,尝试使用window.onload设置click事件,同样作为@halex解决方案,你的n \应该是“\ n”:
window.onload=function() {
document.getElementById("addBid").onclick = addBid;
document.getElementById("removeBid").onclick = removeBid;
}
var bids = [];
var bidders = [];
var bidTime = [];
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.latestBid.value = bidders[0];
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();
if (hours < 10) {
hours = "0" + hours;
}
if (minutes < 10) {
minutes = "0" + minutes;
}
if (seconds < 10) {
seconds = "0" + seconds;
}
var timeText = "[" + hours + ":" + minutes + ":" + seconds + "]";
bidTime.unshift(timeText);
writeBid();
}
function removeBid() {
bidders.shift();
bids.shift();
bidTime.shift();
writeBid();
}
<form name="bidForm" id="bidForm">
<header>
<img src="logo.jpg" alt="Schmitt Auction Haus" />
<nav class="horizontal">
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">Auctions</a></li>
<li><a href="#">Features</a></li>
<li><a href="#">Schedule</a></li>
<li><a href="#">Contacts</a></li>
</ul>
</nav>
</header>
<section id="intro">
<h1>Silent Auction</h1>
<h2>Tawney Farm</h2>
<table id="summarytable">
<tr>
<th>Lot #121</th>
<td>TurfTuff Lawn Tractor X24 (2015)</td>
</tr>
<tr>
<th>Latest Bid</th>
<td>
<input id="latestBid" name="latestBid" readonly="readonly" />
</td>
</tr>
<tr>
<th>Bidding Ends</th>
<td>15:00</td>
</tr>
</table>
<table id="newbidtable">
<tr>
<th colspan="2" id="newtitle">Enter New Bid</th>
</tr>
<tr>
<th>Bidder ID</th>
<td>
<input id="bidId" name="bidId" />
</td>
</tr>
<tr>
<th>Bid Amount</th>
<td>
<input id="bidAmount" name="bidAmount" />
</td>
</tr>
<tr>
<th id="buttons" colspan="2">
<input id="addBid" type="button" value="Submit" />
<input id="removeBid" type="button" value="Remove Last Bid" />
</th>
</tr>
</table>
</section>
<section id="bidHistory">
<h1>Bid History</h1>
<textarea id="bidList" name="bidList"></textarea>
</section>
<footer>
<address>
Schmitt AuctionHaus ·
3401 Rural Route 4 ·
Fenbrook, IN 38012
</address>
</footer>
</form>