夠簡單我猜,但我是一個在這個總新手。我嘗試在PHP中做這個,但後來重新考慮在JS中做這件事,所以我需要一些幫助。 該代碼:JavaScript:列表不顯示
<html>
<head>
<title>Test</title>
<script type="text/javascript">
function reply_click(clicked_id)
{
alert(clicked_id);
var basketItems=new Array();
//var i = 0;
basketItems[1] = clicked_id;
}
</script>
</head>
<body>
<table height="100%" width="80%" cellspacing="10" cellpadding="10" border="1">
<tr>
<td align="center"><input onClick="reply_click(this.name)" name="shirt" value="shirt" style="width:200px; height:100px;" type="submit"/></td>
<td align="center"><input onClick="reply_click(this.name)" name="pants" value="pants" style="width:200px; height:100px;" type="submit"/></td>
<td align="center"><input onClick="reply_click(this.name)" name="socks" value="socks" style="width:200px; height:100px;" type="submit"/></td>
<tr>
<tr>
<td align="center"><input onClick="reply_click(this.name)" name="dress" value="dress" style="width:200px; height:100px;" type="submit"/></td>
<td align="center"><input onClick="reply_click(this.name)" name="skirt" value="skirt" style="width:200px; height:100px;" type="submit"/></td>
<td align="center"><input onClick="reply_click(this.name)" name="topbody" value="topbody" style="width:200px; height:100px;" type="submit"/></td>
<tr>
<tr>
<td align="center"><input onClick="reply_click(this.name)" name="sheets" value="sheets" style="width:200px; height:100px;" type="submit"/></td>
<td align="center"><input onClick="reply_click(this.name)" name="pillowcover" value="pillowcover" style="width:200px; height:100px;" type="submit"/></td>
<td align="center"><input onClick="reply_click(this.name)" name="blanket" value="blanket" style="width:200px; height:100px;" type="submit"/></td>
<tr>
</table>
<div style="width:18%; height:40%;position: absolute; top: 20px; right: 10px; border:2px solid;">
<script type="text/javascript">
var counter=0;
for (counter=0; counter<basketItems.length; counter++)
document.write(basketItems[counter] + "<br>");
</script>
</div>
<br><br><br>
現在我預計會發生。我點擊按鈕,警報出來與點擊按鈕名稱...然後它添加到列表。在div標籤中顯示該列表之後。發生了什麼錯?它只是顯示警報,但沒有顯示在div標籤所以我假設一些錯誤插入到陣列或印刷...
感謝,
UPDATE:此代碼似乎上的jsfiddle工作,但不是我的巴羅什......任何線索有什麼不同?
<html>
<head>
<title>Test</title>
<script type="text/javascript">
var basketItems = [];
function reply_click(clicked_id)
{
alert(clicked_id);
basketItems.push(clicked_id);
var html = '';
for(var i = 0; i < basketItems.length; i++) {
html += basketItems[i] + '<br/>';
}
document.getElementById('list').innerHTML = html;
}
</script>
</head>
<body>
<table height="100%" width="80%" cellspacing="10" cellpadding="10" border="1">
<tr>
<td align="center"><input onClick="reply_click(this.name)" name="shirt" value="shirt" style="width:200px; height:100px;" type="submit"/></td>
<td align="center"><input onClick="reply_click(this.name)" name="pants" value="pants" style="width:200px; height:100px;" type="submit"/></td>
<td align="center"><input onClick="reply_click(this.name)" name="socks" value="socks" style="width:200px; height:100px;" type="submit"/></td>
<tr>
<tr>
<td align="center"><input onClick="reply_click(this.name)" name="dress" value="dress" style="width:200px; height:100px;" type="submit"/></td>
<td align="center"><input onClick="reply_click(this.name)" name="skirt" value="skirt" style="width:200px; height:100px;" type="submit"/></td>
<td align="center"><input onClick="reply_click(this.name)" name="topbody" value="topbody" style="width:200px; height:100px;" type="submit"/></td>
<tr>
<tr>
<td align="center"><input onClick="reply_click(this.name)" name="sheets" value="sheets" style="width:200px; height:100px;" type="submit"/></td>
<td align="center"><input onClick="reply_click(this.name)" name="pillowcover" value="pillowcover" style="width:200px; height:100px;" type="submit"/></td>
<td align="center"><input onClick="reply_click(this.name)" name="blanket" value="blanket" style="width:200px; height:100px;" type="submit"/></td>
<tr>
</table>
<div id="list" style="width:18%; height:40%;position: absolute; top: 20px; right: 10px; border:2px solid;">
</div>
</body>
</html>
和JS小提琴鏈接:http://jsfiddle.net/fVQVy/這裏張貼的解決方案 並感謝大家的耐心......不勝感激....
謝謝你這樣做......我得到第一個項目,但未定義。如果我想保持列表的狀態,那麼當我按下下一個按鈕時,它將添加到列表中而不是重建它?非常感謝 – 2012-04-11 20:59:30
我對未定義的部分進行了排序......只是在0而不是1時插入。其餘的怎麼樣? – 2012-04-11 21:01:47
請參閱更新。另外改變提交按鈕 - 我需要去睡覺。晚上11點。 – mplungjan 2012-04-11 21:03:31