vue-复选框的增加和删除案例

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<style>	
		.addClass {
			text-decoration: line-through;	
		}

	</style>
</head>
<body>
	<div id="app">
		<h1>todo</h1>
		<!-- 选出todos里面,没有被选择的复选框,计算它的唱的 -->
		<p>{{todos.filter(item => !item.done).length}} of {{todos.length}} remaining [archive]</p>
		<p v-for="(item, index) in todos">
			<!-- 鼠标enter键抬起也可以添加 不过为啥不起作用。。。。难办-->
			<input type="checkbox" v-model="item.done" @keyup.enter="add">
			<!-- v-bind 为元素添加类,addClass 为类名,后面是个boolean类型,为true则添加这个类,false则不添加 -->
			<span v-bind:class="{addClass: item.done}">{{item.title}}</span>
			<button @click="remove(index)" >删除</button>
		</p>
		<input type="text" v-model="text"><button @click="add">add</button>
	</div>
	<script src="node_modules/vue/dist/vue.js"></script>
	<script>
	var todos = [
		{
			id: 1,
			title: '吃饭',
			done: false
		},
		{
			id: 2,
			title: '睡觉',
			done: true
		},
		{
			id: 3,
			title: '打豆豆',
			done: false
		}
	]

	var app = new Vue({
		el: '#app',
		data: {
			todos,
			text: ''

		},
		methods: {
			add: function () {
				if( !this.text) {
					return
				}
				var text = this.text.trim()
				var adds = {
					id: this.todos[this.todos.length-1].id + 1,
					title: text,
					done: false
				}
				todos.push(adds)
				//添加完毕之后输入框清空
				this.text = ''
			},
			remove: function (index) {
				todos.splice(index, 1)
			}
		}
	})

	</script>
</body>
</html>

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值