时间:2021-05-26
本文实例为大家分享了vue实现单选和多选功能的具体代码,供大家参考,具体内容如下复制代码
vue实现多选功能
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta> <title>Document</title> <script src="../vue.js"></script> <style> ul, li { list-style-type: none; } * { margin: 0; padding: 0; } .border-1px { position: relative; } .border-1px:after { display: block; position: absolute; left: 0; bottom: 0; width: 100%; border-top: 1px solid rgba(7, 17, 27, .1); content: ' '; } @media (-webkit-min-device-pixel-ratio: 1.5),(min-device-pixel-ratio: 1.5) { .border-1px::after { -webkit-transform: scaleY(0.7); transform: scaleY(0.7); } } @media (-webkit-min-device-pixel-ratio: 2),(min-device-pixel-ratio: 2) { .border-1px ::after { -webkit-transform: scaleY(0.5); transform: scaleY(0.5); } } #example { margin: 20px; } h3 { font-size: 26px; margin-left: 20px; height: 60px; } .self-checkbox { display: none; } .self-checkbox + label { margin-top: 16px; -webkit-appearance: none; background-color: #fff; border: 2px solid #aaa; border-radius: 5px; display: inline-block; position: relative; width: 30px; height: 30px; box-sizing: border-box; vertical-align: top; } .self-checkbox:checked + label { border: 2px #47d9bf solid; } .self-checkbox:checked + label:after { display: inline-block; text-align: center; content: '√'; width: 100%; height: 30px; line-height: 26px; color: #47d9bf; font-size: 18px; text-shadow: 0px 0px 5px #47d9bf; } .check-area { display: inline-block; width: 400px; padding: 12px 20px; border: 1px solid #aaa; border-top-left-radius: 4px; border-top-right-radius: 4px; } li { height: 60px; } li .self-radio + label { vertical-align: middle; } li span { margin-left: 20px; display: inline-block; line-height: 60px; font-size: 22px; } p { height: 60px; line-height: 60px; margin-left: 20px; } p span { color: #f00; } .btn { margin: 20px auto; width: 100%; text-align: center; } .btn button { width: 120px; height: 40px; line-height: 30px; font-size: 16px; color: #fff; background: #47d9bf; border: 1px #23d5b6 solid; border-radius: 6px; text-align: center; outline: none; } .btn button:hover { background: #23d5b6; } </style></head><body><div id="example"> <h3>多选按钮</h3> <div class="check-area" v-show="items.length!=0"> <ul> <li class="border-1px" v-for="(item,index) in items"> <input class="self-checkbox" type="checkbox" :id="'checkbox-'+item.id" :data-id="'food-'+item.id" name="radio" :value="item.value" v-model="checkValues" @click="setCheckValue($event,item)"> <label :for="'checkbox-'+item.id"></label> <span>{{item.value}}</span> </li> </ul> <p>您选择了:<span v-show="checkValues.length">{{filterCheckValues}}</span></p> <div class="btn"> <button @click="showCheck(checkIds)">按钮</button> <span v-show="checkIds.length">{{checkIds}}</span> </div> </div></div><script> var itemData = [{id: '20170811001', value: '香蕉'}, {id: '20170811002', value: '苹果'}, { id: '20170811003', value: '梨子' }, {id: '20170811004', value: '葡萄'}] //itemData = []; var vm = new Vue({ el: '#example', data: { items: '', checkValues: [], checkIds: [] }, computed: { filterCheckValues: function () { var value = this.checkValues; var reValue = ''; for (var i = 0; i < value.length; i++) { reValue += value[i] + '、' } reValue = reValue.substring(0, reValue.length - 1) return reValue; } }, methods: { initData: function () { var self = this; self.items = itemData; if (itemData.length != 0) {// self.checkValues[0] = self.items[0].value;// self.checkIds[0] = 'food-' + self.items[0].id; } }, setCheckValue: function (ev, item) { var id = 'food-' + item.id; if (ev.target.checked) { this.checkIds.push(id); } else if (this.checkIds.indexOf(id) > -1) { this.checkIds.remove(id); } } , showCheck: function () { console.log(this.checkIds) } }, filter: {}, mounted: function () { this.initData(); } }) Array.prototype.remove = function (val) { var index = this.indexOf(val); if (index > -1) { this.splice(index, 1); } };</script></body></html>以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
vue实现div单选多选功能,供大家参考,具体内容如下单选单选:多选:单选功能DOM层:0-16岁17-25岁26-35岁36-45岁46-55岁55岁以上js
本来实现多选单选这个功能,vue组件中在表单方面提供了一个v-model指令,非常的善解“猿”意,能把我们的多选单选功能很完美的且很强大得双向绑定起来,实现多选
本文实例讲述了基于javascript实现单选及多选的向右和向左移动实例。分享给大家供大家参考。具体实现方法如下:方法一:实现单选及多选的向右和向左移动选项1选
本文介绍了ReactJS实现表单的单选多选和反选的示例,分享给大家,希望对大家有所帮助。需求是对列表实现单选,反选和多选,全部清除的操作......this.s
今天在项目中遇到了tableview的单选需求,现在总结一下,用一个简单的demo实现了简单的单选和多选两个功能.先看下效果图:1:首先实现下单选1:使用一个变