
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 your list
if (counter != 0) counter--// updates the counter
show()
}
function sortlist() {
goods.sort()// sorts the list alphabetically from A to Z
show()
}
function reverselist() {
goods.reverse()// sorts the list alphabetically from Z to A
show()
}
</script>
<h2>My Shopping List</h2>
<div>Enter an item to add in your list:
<input type="text" name="add" value=""/>
</div>
<div><input type="button" name="submit" value="Add to my List" onclick="addList()"/>
<input type="button" name="reset" value="reset" onclick="resetList()"/>
</div>
Number of items in your list:
<input type="text" name="number" value="0"/>
<div><b>Items you need to buy at the store:</b></div><br>
<input type="text" name="item" value="" style="width:450px"/>
<div>
<input type="button" name="remove" value="remove" onclick="remove()"/>
<input type="button" name="" value="sortlist" onclick="sortlist()"/>
<input type="button" name="reset" value="reverselist" onclick="reverselist()"/>
</div>
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)