在以下 vue.js 示例中,如果 myBool
的值为 null
,复选框不会返回 N
。
<input v-model="myBool" true-value="Y" false-value="N" type="checkbox">
但是,如果我将 myBool
设置为 false
,复选框将返回 N
。即使 myBool
为 null
,如何获取值 N
?
我不想手动检查复选框的返回值,然后添加逻辑使其为假。当myBool
为null
时,是否有更好的方法来获取N
值?
最佳答案
我还没有尝试过这个,但也许将 v-model 设置为 Computed Setter
computed: {
myBoolComputed: {
// getter
get: function () {
return this.myBool ? true : false
},
// setter
set: function (newValue) {
this.myBool = newValue
// or if preferred to stay strictly Boolean
// this.myBool = (newValue === 'Y')
}
}
}
模板
<input v-model="myBoolComputed" true-value="Y" false-value="N" type="checkbox">
关于vuejs2 - 当 v-model 为 null 时,在 Vue.js 中将复选框值设置为 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47166863/