vue实现单选和多选功能


Posted in Javascript onAugust 11, 2017

本文实例为大家分享了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-radio {
   display: none;
  }

  .self-radio + label {
   -webkit-appearance: none;
   background-color: #fff;
   border: 1px solid #aaa;
   border-radius: 50px;
   display: inline-block;
   position: relative;
   width: 30px;
   height: 30px;
   box-sizing: border-box;
  }

  .self-radio:checked + label {
   border: 1px #47d9bf solid;
  }

  .self-radio:checked + label:after {
   position: absolute;
   top: 9px;
   left: 9px;
   content: ' ';
   width: 10px;
   height: 10px;
   border-radius: 50px;
   background: #47d9bf;
   box-shadow: 0px 0px 5px 0px #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-radio" type="radio"
      :id="'radio-'+item.id"
      :data-id="'food-'+item.id" name="radio"
      :checked="index==0"
      :value="item.value"
      v-model="checkValue">
    <label :for="'radio-'+item.id" @click="setCheckValue(item)"></label>
    <span>{{item.value}}</span>
   </li>
  </ul>
  <p>您选择了:<span>{{checkValue}}</span></p>
  <div class="btn">
   <button @click="showCheck(checkId)">按钮</button>
   <span>{{checkId}}</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: '',
   checkValue: '',
   checkId: ''
  },
  methods: {
   init: function () {

   },
   initData: function () {
    var self = this;
    self.items = itemData;
    if (itemData.length != 0) {
     self.checkValue = self.items[0].value;
     self.checkId = 'food-' + self.items[0].id
    }
   },
   setCheckValue: function (item) {
    this.checkId = 'food-' + item.id;
   }
   ,
   showCheck: function () {
    console.log(this.checkId)
   }
  },
  mounted: function () {
   this.initData();
  }
 })

</script>
</body>

</html>

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>

vue实现单选和多选功能

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
JavaScript类和继承 constructor属性
Mar 04 Javascript
Javascript之旅 对象的原型链之由来
Aug 25 Javascript
javascript中this做事件参数相关问题解答
Mar 17 Javascript
JavaScript按位运算符的应用简析
Feb 04 Javascript
jQuery学习笔记之jQuery原型属性和方法
Jun 09 Javascript
教你如何使用node.js制作代理服务器
Nov 26 Javascript
JavaScript常用字符串与数组扩展函数小结
Apr 24 Javascript
AngularJS使用ocLazyLoad实现js延迟加载
Jul 05 Javascript
vue用Object.defineProperty手写一个简单的双向绑定的示例
Jul 09 Javascript
Vue Router去掉url中默认的锚点#
Aug 01 Javascript
Cocos2d实现刮刮卡效果
Dec 20 Javascript
Django+Vue实现WebSocket连接的示例代码
May 28 Javascript
js微信应用场景之微信音乐相册案例分享
Aug 11 #Javascript
Angular模板表单校验方法详解
Aug 11 #Javascript
AngularJs导出数据到Excel的示例代码
Aug 11 #Javascript
Vue 表单控件绑定的实现示例
Aug 11 #Javascript
Angular4实现动态添加删除表单输入框功能
Aug 11 #Javascript
node中使用es5/6以及支持性与性能对比
Aug 11 #Javascript
ionic2屏幕适配实现适配手机、平板等设备的示例代码
Aug 11 #Javascript
You might like
在Windows中安装Apache2和PHP4的权威指南
2006/10/09 PHP
php+mysql实现数据库随机重排实例
2014/10/17 PHP
php创建类并调用的实例方法
2019/09/25 PHP
Extjs gridpanel 出现横向滚动条问题的解决方法
2011/07/04 Javascript
基于jquery的web页面日期格式化插件
2011/11/15 Javascript
JavaScript 表单处理实现代码
2015/04/13 Javascript
jQuery实现简单的文件上传进度条效果
2020/03/26 Javascript
JavaScript生成二维码图片小结
2015/12/27 Javascript
jQuery.deferred对象使用详解
2016/03/18 Javascript
教大家轻松制作Bootstrap漂亮表格(table)
2016/12/13 Javascript
js多个物体运动功能实例分析
2016/12/20 Javascript
jQuery鼠标悬停内容动画切换效果
2017/04/27 jQuery
微信小程序 同步请求授权的详解
2017/08/04 Javascript
Angular中响应式表单的三种更新值方法详析
2017/08/22 Javascript
AngularJS 的$timeout服务示例代码
2017/09/21 Javascript
javascript+html5+css3自定义弹出窗口效果
2017/10/26 Javascript
JavaScript EventEmitter 背后的秘密 完整版
2018/03/29 Javascript
详解js跨域请求的两种方式,支持post请求
2018/05/05 Javascript
原生js实现自定义滚动条
2021/01/20 Javascript
Python不规范的日期字符串处理类
2014/06/10 Python
基于python3实现socket文件传输和校验
2018/07/28 Python
python 实现turtle画图并导出图片格式的文件
2019/12/07 Python
浅谈PyQt5中异步刷新UI和Python多线程总结
2019/12/13 Python
python实现将字符串中的数字提取出来然后求和
2020/04/02 Python
Python使用for生成列表实现过程解析
2020/09/22 Python
详解python算法常用技巧与内置库
2020/10/17 Python
Pytorch之扩充tensor的操作
2021/03/04 Python
欢迎领导检查标语
2014/06/27 职场文书
售后客服个人自我评价
2014/09/14 职场文书
群众路线专项整治方案
2014/10/27 职场文书
党校学习个人总结
2015/02/15 职场文书
教师考核鉴定意见
2015/06/05 职场文书
你会写请假条吗?
2019/06/26 职场文书
导游词之桂林山水
2019/09/20 职场文书
MongoDB balancer的使用详解
2021/04/30 MongoDB
Anaconda安装pytorch和paddle的方法步骤
2022/04/03 Python