javascript怎样用array来实现每次input就加上一项东西啊
来源:百度知道 编辑:UC知道 时间:2024/06/18 05:47:25
据要求是要用array来实现.大致画面如下:
My Shopping List
Enter an item to add in your list: [ input ]
(Add to my List)(Reset)---两个button
Number of items in your list: [ 3 ]
Items you need to buy at the store:
[apples,oranges,potatoes ]
新手初学啊~老师又讲不清楚...(也可能是我个人理解能力有问题)反正,烦请各位高手帮忙拉~~~谢谢~
作业具体要求:
Add to my list的function:
1.uses an array variable to store the information/items from the page
2.needs a global variable "counter" to track the last index number
3.it will display the number of items stored in the array
4.it will display the content of the array
Reset的function:
1.resets the "counter" to zero
2.resets the array by reinitializing the array
另外还要加三个function:
分别是remove()---removes the last item on your list, updates the counter,
sortlist()-
<script>
var goods = new Array(), // 保存商品的数组
counter = 0; // 商品数目
function addList() {
var o = document.getElementById('add');
// 没有输入商品
if (o.value.length == 0) {
alert('please enter an item!');
return false;
}
goods.push(o.value); // 添加商品到数组
counter++; // 商品数目加一
show();
}
function resetList() {
counter = 0; // resets the "counter" to zero
goods.length = 0; // resets the array by reinitializing the array
show();
}
// 页面处理
function show() {
document.getElementById('add').setAttribute('value', ''); // 清空输入
document.getElementById('number').setAttribute('value', counter); // 页面显示商品数目
document.getElementById('item').setAttribute('value', goods); // 页面显示商品
}
function remove() {
goods.pop(); // removes the last item on