vue的小练习

<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<title></title>
		<script type="text/javascript" src="vue.js"></script>

	</head>
	<style type="text/css">
		#app {
			margin: 50px auto;
			width: 500px;
			
		}
		fieldset{
			border: 1px solid orange;
		}
		fieldset input{
			width: 400px;
			height: 30px;
			margin: 10px 0;
			
		}
		table{
			width: 500px;
			border: 1px solid orange;
			text-align: center;
			margin-top: 40px;
		}
		thead{
			background-color: orange;
			color: white;
		}
		button{
			width: 90px;
			height: 40px;
			background-color: rosybrown;
		}
	</style>

	<body>
		<div id="app">
		<!--第一部分-->
          <fieldset>
          	<legend>小拇指学生录入系统</legend>
          	<div>
          		<span>姓名:</span>
          		<input type="text" placeholder="请输入姓名" v-model="newStudent.name"/>
          	</div>
          		<div>
          		<span>年龄:</span>
          		<input type="text" placeholder="请输入年龄" v-model="newStudent.age"/>
          	</div>
          		<div>
          		<span>性别:</span>
          		<select name="" v-model="newStudent.sex">
          			<option value="男">男</option>
          			<option value="女">女</option>
          		</select>
          	</div>
          		<div>
          		<span>手机:</span>
          		<input type="text" placeholder="请输入手机号码"/ v-model="newStudent.phone">
          	</div>
          	<button @click="createNewStudent()">创建新用户</button>
          </fieldset>
         <!--第一部分--> 
          <!--第二部分--> 
          <table border="" cellspacing="" cellpadding="">
          	<thead>
          			<tr>
          				<th>姓名</th>
          				<th>性别</th>
          				<th>年龄</th>
          				<th>手机号</th>
          				<th>删除</th>
          			</tr>
          	</thead>
            <tbody>
            	  <tr v-for='(p, index) in persons'>
            	  	   <td>{{p.name}}</td>
            	  	     <td>{{p.sex}}</td>
            	  	       <td>{{p.age}}</td>
            	  	         <td>{{p.phone}}</td>
            	  	         <td><button @click="deleteStudents(index)">删除</button></td>
            	  </tr>
            </tbody>
          	
          </table>
           <!--第二部分--> 
		</div>
			<script type="text/javascript">
				new Vue({
					el: '#app',
					data: {
						persons: [{
								name: '张三',
								age: 19,
								sex: '男',
								phone: 17834783784
							},
							{
								name: 'lisi',
								age: 31,
								sex: '男',
								phone: 1786666784
							},
							{
								name: '王五',
								age: 22,
								sex: '女',
								phone: 1783477777
							},
							{
								name: '赵四',
								age: 29,
								sex: '男',
								phone: 178347446544
							},
							{
								name: '马云',
								age: 49,
								sex: '男',
								phone: 17834786666
							}
						],
						newStudent:{name:'',age:0,sex:'',phone:''}
					},
					methods:{
						createNewStudent(){
							//姓名不能为空
							if(this.newStudent.name===''){
								alert('姓名不能为空');
								return;
							}
							if(this.newStudent.age<=0){
								alert('请输入正确的年龄');
								return;
							}
							
							if(this.newStudent.phone===''){
								alert('手机不能为空');
								return;
							}
							//往数组添加一条新数据
							this.persons.unshift(this.newStudent)
							//清空数据
							this.newStudent={name:'',age:0,sex:'',phone:''}
						},
						//删除一条学生记录
					deleteStudents(index){
						this.persons.splice(index,1);
						
					}
					}
					
				})
			</script>
	</body>

</html>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值