开始写这个功能,不得不吐槽原始的checkbox,灰色小方块的丑陋,虽说eleUI,mintUI,等各种框架的单复选框已经对其优化,但还是不想要这种。那我们就来研究一下怎么处理它。

 <section class="box">
<label :for="item3" @click="chooseType($event,index3)" v-for="(item3,index3) in type" class="labelName">
<input type="checkbox" :value="item3" :id="item3" v-model="checkedValue" class="checkboxList"> // for属性一定要与id一致,原因请看上图
<div class="name">{{item3}}</div> // label的值 // checkbox的v-model绑定值一定要是数组
&nbsp;&nbsp;
{{checkedValue}} // 查看值
</label>
<button @click="chooseQu">全选</button>
<button @click="chooseNo">全不选</button> </section>

data:

data(){
return{
checkedValue: [],
type:['a','b','c','d'] // 测试数据,可在mounted钩子函数中将后台数据赋值
}
},

methods:

methods:{
chooseType(e,val){
console.log(e.target.checked) // 可获取当前的checked 状态
console.log(val) // 可获取到当前的下标。 },
chooseQu(){
// document.querySelectorAll('.checkboxList').setAttribute("checked","true");
this.checkedValue = this.type ; //将原数据全部赋值给checkedValue,全部置为true.
},
chooseNo(){
this.checkedValue = [] ; // checkedValue 为空即将checkedValue全部置为false,
} }

样式的调整:

// 样式可根据自己的需要,通过控制外层div自行配置,
.box{
/*display: flex;*/
/*justify-content: start;*/
/*align-items: center;*/
}
.labelName{
width: 25%;
display: flex;
justify-content: center;
margin-bottom: 20px;
}
/*------新-----*/
input[type=checkbox] {
width: 20px; // 可根据自己的需要进行配置input的大小。
height: 20px;
border-radius: 10px;
-webkit-appearance: none;
background-color: transparent;
border: 0;
outline: 0 !important;
color: #d8d8d8;
position: relative;
}
input[type=checkbox]:before{
content: "";
display:block;
width: 20px;
height: 20px;
border-radius: 10px;
border: 1px solid #ddd;
background-color: #fff;
box-sizing:border-box;
position: absolute;
} input[type=checkbox]:disabled:before{
content: "";
display:block;
width: 20px;
height: 20px;
border-radius: 10px;
border: 1px solid #333;
background-color: #333;
box-sizing:border-box; // 可控制调整背景色。
position: absolute;
}
input[type=checkbox]:checked:before{
content: "";
display:block;
width: 20px;
height: 20px;
border-radius: 10px;
border: 1px solid #D2A47E;
background-color: #D2A47E; //可控制checked背景色
box-sizing:border-box;
position: absolute;
}
input[type=checkbox]:checked:after{
content: "";
display:block;
/*width: .15rem;*/
/*height: .3rem;*/
/*border-radius: .06rem;*/
width: 7px; // 此处是控制获取checked=true 后的颜色,请注意宽高比约1:2 。 原理是通过伪类去控制样式。
height: 15px;
/*border-radius:3px;*/
border-left: .07rem solid #fff;
border-top: .07rem solid #fff;
box-sizing:border-box;
position: absolute;
transform: rotate(-135deg) translate(-70%, 25%);
}

vue中checkbox 样式自定义重写;循环遍历checkbox,拿到不同的v-model绑定值;及获取当前checked 状态,全选和全不选等功能。的更多相关文章

  1. Vue学习笔记七:Vue中的样式

    目录 两种样式 class样式 内联样式 两种样式 Vue中使用样式方式有两种,一种是class样式,一种是内联样式也就是style class样式 class样式使用的方式有5种,HTML如下 &l ...

  2. 3-5 Vue中的样式绑定

    Vue中的样式绑定: 本案例,简单设计一个<div>的点击绑定事件来改变div的样式效果 方法一:[class] ①(class和对象的绑定) //如上,运用class和一个对象的形式来解 ...

  3. VUE中CSS样式穿透

    VUE中CSS样式穿透 1. 问题由来 在做两款H5的APP项目,前期采用微信官方推荐的weui组件库.后来因呈现的效果不理想,组件不丰富,最终项目完成后全部升级采用了有赞开发的vant组件库.同时将 ...

  4. vue.js(7)--vue中的样式绑定

    vue中class样式与内联样式的绑定 <!DOCTYPE html> <html lang="en"> <head> <meta cha ...

  5. 深入理解 vue 中 scoped 样式作用域的规则

    哈喽!大家好!我是木瓜太香,今天我们来聊一个 vue 的样式作用域的问题,通常我们开发项目的时候是要在 style 上加上 scoped 来起到规定组件作用域的效果的,所以了解他们的规则也是很有必要的 ...

  6. JavaScript 中的常用12种循环遍历(数组或对象)的方法

    1.for 循环 let arr = [1,2,3]; for (let i=0; i<arr.length; i++){ console.log(i,arr[i]) } // 0 1 // 1 ...

  7. vue组件,vue补充和总结,JS循环遍历和加减运算、类型转换补充

    目录 一.vue中的组件 1. 组件的概念 2. 组件分类 3. 组件的特点 4. 组件的定义 5. 组件化 (1)用法和注意 (2)数据组件化实例 6. 组件传参--父传子 (1)用法和注意 (2) ...

  8. vue中动态样式不起作用? scoped了解一下

    vue中style标签使用属性scoped的注意事项 style上添加属性scoped可以实现样式私有化,但是在使用动态样式时,样式会不起作用.可以先去掉scoped

  9. javascript中常见的几种循环遍历

    项目开发中,不管是建立在哪个框架基础上,对数据的处理都是必须的,而处理数据离不开各种遍历循环.javascript中循环遍历有很多种方式,记录下几种常见的js循环遍历. 一.for循环 for循环应该 ...

随机推荐

  1. PTA 紧急救援 /// dijkstra 最短路数 输出路径

    题目大意: 给定 n m s t :表示n个点编号为0~n-1 m条边 起点s终点t 接下来一行给定n个数:表示第i个点的救援队数量 接下来m行给定u v w:表示点u到点v有一条长度为w的边 求从s ...

  2. JavaScript — event介绍以及兼容处理

    JavaScript - event介绍以及兼容处理 1.事件流 浏览器发展到第四代时(IE4及 Netscape Communicator 4),浏览器开发团队遇到一个问题:页面的哪个部分会拥有某个 ...

  3. 11.Container With Most Water (Array; Two-Pointers)

    Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai).  ...

  4. JQuery 获取表格table所有行第一列

    main_table 是表ID $("#main_table tr").find("td:eq(0)")

  5. 2019-9-29-dotnet-对-DateTime-排序

    title author date CreateTime categories dotnet 对 DateTime 排序 lindexi 2019-09-29 14:55:49 +0800 2019- ...

  6. Python 第三天学习整理①

    今天学习的内容包括以下几个方面:1.字符编码 2.文件操作 3.函数 1.字符编码 关于字符编码,关注以下几个点 1.1 什么是字符编码 字符编码:字符转化为数字的过程所遵循的标准 字符编码包括:un ...

  7. nodejs包高效升级插件npm-check-updates

    一.安装 npm install -g npm-check-updates 或 cnpm install -g npm-check-updates 二.使用 ncu crypto ^0.0.3 → ^ ...

  8. python 读 xlsx

    前言 xlsx写方法参考此连接:http://www.cnblogs.com/whf191/p/5482485.html xlrd是用来读的,使用前需安装 pip install xlrd 例子 fn ...

  9. 【C/C++】知识点系统复习 (第一周)

    2018/12/18 周二 1. C++内存布局分为几个区域,每个区域有什么特点? 主要可以分为 5 个区域, (1) 栈区:由编译器自动分配释放,存放函数的参数值,局部变量的值等.其操作方式类似于数 ...

  10. C# string.Format json格式字符串报错”输入字符串的格式不正确“

    当我们在string.Format中传入Json字符串时,会报”输入字符串的格式不正确“,这是因为json的"{"符号的问题,最开始我是想着用转义一下"{",但 ...