zoukankan      html  css  js  c++  java
  • vue checkbox 双向绑定及初始化渲染

    双向绑定可以绑定到同一个数组
    <input type="checkbox" id="jack" value="Jack" v-model="checkedNames">
    <label for="jack">Jack</label>
    <input type="checkbox" id="john" value="John" v-model="checkedNames">
    <label for="john">John</label>
    <input type="checkbox" id="mike" value="Mike" v-model="checkedNames">
    <label for="mike">Mike</label>
    
    <span>Checked names: {{ checkedNames | json }}</span>
    这样可以不做任何处理,获得所有checked的checkbox。

    可是如果需要初始化渲染,一部分checkbox被预选中,v-model需要绑定一个boolen值checked:(此时checked为数字类型的0或1也是可以的)
    <tr v-for="item in list">
        <td>
            <input type="checkbox" :value="item.value" v-model="item.checked" />
        </td>
    </tr>
    
    new Vue({
        el: 'body',
        data: {
            list: [{checked:true,value:'a'},{checked:false,value:'b'}]
        }
    });
    然后利用 filter, map 来完成
    methods: {
        getChecked: function() {
            // 先透过 filter 筛选出 checked 为 true 的 item
            // 在透过 map 只回传 item 的 value
            return this.list.filter(item => item.checked).map(item => item.value)
        }
    }
  • 相关阅读:
    闰年的定义
    Sublime Text 3
    维特比算法(Viterbi)
    索引
    倒排索引
    URL过滤
    判断一个元素是否在集合中
    布隆过滤器
    jsp九大内置对象
    jsp九大内置对象和其作用详解
  • 原文地址:https://www.cnblogs.com/happyhaibei/p/6875469.html
Copyright © 2011-2022 走看看