图书管理系统 二维数组或对象

<html>
	<head>
		<meta charset="utf-8">
		<title></title>

	</head>
	<body>
		<p align="center">
			<span>书名</span><input type="text" id="userName" />
			<span>作者</span><input type="text" id="userAuthor" />
			<span>价格</span><input type="text" id="userPrice" />
			<span>数量</span><input type="text" id="userCount" />
			
		</p>
		<p align='center'>
			<button id="add">添加</button>
			<button id="query">查询</button>
		</p>
		<hr />
		<div id="showMessage"></div>
		
		<script type="text/javascript">
			    var userNameInput = document.getElementById('userName');
			    var userAuthorInput = document.getElementById('userAuthor');
			    var userPriceInput = document.getElementById('userPrice');
			    var userCountInput = document.getElementById('userCount');
			    var addBtn = document.getElementById('add');
			    var queryBtn = document.getElementById('query');
			    var showDiv = document.getElementById('showMessage');
			    //1.5准备二维数组 用于承载数据
			    var bookArr = [];
			    //2.给button添加点击事件
			    //2.1添加按钮
			    addBtn.onclick = function () {
			        //2.10获取元素内容
			        var userName = userNameInput.value;
			        var userAuthor = userAuthorInput.value;
			        var userPrice = userPriceInput.value;
			        var userCount = userCountInput.value;
			        //2.11对元素内容判断
			        if (userName.trim().length == 0 || userAuthor.trim().length == 0 || userCount.trim().length == 0 || userPrice
			                        .trim().length == 0) {
			            //2.12元素内容不完整
			            alert('数据不完整请您将数据补充完整');
			        }else{
			        	/*var arr=[];  使用数组
			        	arr.push(userName);
			        	arr.push(userAuthor);
			        	arr.push(userPrice);
			        	arr.push(userCount);*/
			        	
			        	/*使用对象*/
			        	var obj={};
			        	obj.userName=userName;
			        	obj.userAuthor=userAuthor;
			        	obj.userPrice=userPrice;
			        	obj.userCount=userCount;
			        	
			        /*	bookArr.push(arr); 二维数组*/
			       
			        	bookArr.push(obj);
			        	
			        	userAuthorInput.value="";
			        	userCountInput.value="";
			        	userPriceInput.value="";
			        	userNameInput.value="";
			        	
			        	console.log(bookArr);
			        }
			                        
			    }
				
				queryBtn.onclick=function()
				{
					if(bookArr.length==0)
					{
						alert("无数据");
					}else{
						var str="<table border='1' cellspacing='0px' width='600px' align='center' style='text-align:center;'>"+
								"<tr><td>书名</td><td>作者</td><td>价格</td><td>数量</td></tr>";
						
				/*使用数组		for(var i=0;i<bookArr.length;i++)
						{
							str+="<tr><td>"+bookArr[i][0]+"</td><td>"+bookArr[i][1]+"</td><td>"+bookArr[i][2]+"</td><td>"+bookArr[i][3]+"</td></tr>";
							
							
						}*/
						
								for(var i=0;i<bookArr.length;i++)
						{
							str+="<tr><td>"+bookArr[i].userName+"</td><td>"+bookArr[i].userAuthor+"</td><td>"+bookArr[i].userPrice+"</td><td>"+bookArr[i].userCount+"</td></tr>";
							
							
						}
						
						str+="</table>";
						showDiv.innerHTML=str;
					}
				}
			
		</script>
	</body>
</html>
      
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值