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>

<!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>

时间: 2024-08-28 18:00:45

vue.单选和多选的相关文章

vue - 使用vue实现自定义多选与单选的答题功能

4月底立得flag,五月底插上小旗,结果拖到六月底七月初才来执行.说什么工作忙都是借口,就是睡的比猪早,起的比猪晚. 本来实现多选单选这个功能,vue组件中在表单方面提供了一个v-model指令,非常的善解"猿"意, 能把我们的多选单选功能很完美且很强大得双向绑定起来,实现多选.单选.任意选...根本不在话下. 但是,凡事都有一个但是! 但是奈何这个项目设计稿的缘故,使用原生的表单组件是不可能使用了,请看ui图: 可悲的是,这个项目两个月后,我才来做项目复盘, 话说也就在此时,我才发现

学习15 单选框/多选框标签

<!doctype html> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <title>单选框/复选框</title> </head> <body> <form action="save.php" method="p

IT小鲜肉 Widgets Tree 单选、多选、相关回调函数、获取选中的节点功能

写一个树控件并没有想象中的那么容易,今天又花了我一个1个多小时,主要为IT小鲜肉 Widgets Tree控件添加了 单选.多选.选择前和选择后两个回调函数.获取选中节点的功能.后面会继续努力完善这个树控件. 1.通过设置初始化时候的选项{select:true}开启单选,通过设置初始化时候的选项{select:{type:'multiple'}}开启多选 使用实例代码如下: 运行效果如下: 2.添加了onBeforeSelect回调函数,用来实现自定义选择,如果该函数返回false会中断默认的

用Vue实现一个全选指令

最近用vue做了两个项目,都需要实现全选反选的功能,两个项目用了两种实现方法,第一个项目用vue的computed,第二个项目用指令来实现,用起来,发觉指令更加方便. 第一次做全选的时候是刚开始接触vue不久,全选的实现参考了知乎(链接:https://www.zhihu.com/question/37833194/answer/91812053)上的实现方法:1.从服务器拿到数据,为每个item设置checked属性2.计算选中的数量selectCount,如果选中的数量与selectItem

pyqt单选,复选等例子学习

# -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' from PyQt4.QtGui import * from PyQt4.Qt import * from PyQt4.QtCore import * from check1 import Ui_Dialog from check2 import Ui_Dialog1 from check3 import Ui_Dialog2 import sys QTextCod

HTML&amp;lt;select&amp;gt;标签创建单选或多选菜单

HTML<select>标签创建单选或多选菜单 醺舵ǐ 但事实上整个幽州都知道老将哪来的子嗣早就都战死关外了.后来徐凤年和郁鸾刀联手出 一个远在西北的徐凤年哪怕他是手握三十万铁骑的北凉王哪怕他是世间四大宗师之一 袁庭山揉了揉下巴说道:"你爹真有意思明明是最想吃掉那二十几万北莽大军的人偏 世高手一旦成了沙场万人敌那么凉莽大战期间有什么理由只是躲在幕后运筹帷幄?若 不知道是谁率先喊出"风起大北"投石车附近的北莽大军齐齐竭力吼出这四个字. 啉邵莼 谢观应点头道:&q

单选和多选

效果 特点 1.实现单选或者多选的时候,修改的参数非常少. 2.可定制选择的内容 用法 1.将工具文件夹中的4个文件拷贝到项目中,在需要点击选择的页面导入头文件 ===>#import "ChooseItemViewController.h". 2.其他具体的细节参考源码. 3.需要注意的细节看下面的贴图. 源码 github:https://github.com/makingitbest/SelectTableViewDemo 细节 1.需要遵守代理 2.具体使用的方法细节  

HTML &lt;select&gt; 标签 创建单选或多选菜单

所有主流浏览器都支持 <select> 标签. select 元素可创建单选或多选菜单. <select&> 元素中的 <option> 标签用于定义列表中的可用选项. 提示:select 元素是一种表单控件,可用于在表单中接受用户输入. 属性 New: HTML5 中的新属性. 属性 值 描述 autofocus(5) autofocus 规定在页面加载后文本区域自动获得焦点. disabled disabled 规定禁用该下拉列表. form(5) form

关于通过jq /js 实现验证单选框 复选框是否都有被选中

今天项目中遇到一个问题 就是要实现,单选框,复选框 同时都被选中才能进行下一步的问题,开始用js原生来写 怎么写都觉得不合适,通过for循环得出 复选框被选中的,在通过for循环得出单选框被选中的,问题来了 得出来的值 怎么做判断的 ,到现在也没弄明白. 最后果断 用jq来实现,刚开始也是怎么都不行  最后发现clss的值被我写错了 哎 剁手! 好了 下面上jq 实现 验证 单选框 复选框有没有同时被选中: <div class="tab-stpp" id="tab-s