【发布时间】:2021-12-26 12:23:54
【问题描述】:
我试图弄清楚如何正确镜像两个输入字段并根据复选框值动态更改它们。这是我能想到的将计算属性与 get 和 set 功能结合使用的最好方法。 问题是当用户在运输详细信息中键入 (1)“ABC”,(2) 取消选中 (copy_chekcbox=false),(3) 在帐单详细信息中键入“123”,以及 (4) 复查 (copy_chekcbox=true) ( 5) 帐单详细信息不会恢复为运输详细信息,这是我需要的。 Image with steps
另外,是否有任何更短、更优化的方法来镜像两个字段并根据复选框值更改它们的值?
提前谢谢你。
<!DOCTYPE html>
<html>
<head>
<title>My first Vue app</title>
<script src="https://unpkg.com/vue"></script>
</head>
<body>
<div id="app">
<p>Shipping details</p>
<input v-model="shipping" type="text" />
<p>Same as shipping: <input type="checkbox" v-model="copy_chekcbox" /></p>
<p>Billing details</p>
<input v-model="billing" type="text" :disabled="copy_chekcbox" />
</div>
<script>
var app = new Vue({
el: '#app',
data: {
shipping_data: '',
billing_data: '',
copy_chekcbox: true,
},
computed: {
shipping: {
get: function() {
return this.shipping_data
},
set: function(newValue) {
if (!this.copy_chekcbox) {
this.shipping_data = newValue
} else {
this.billing_data = newValue
this.shipping_data = newValue
}
},
},
billing: {
get: function() {
return this.billing_data
},
set: function(newValue) {
if (!this.copy_chekcbox) {
this.billing_data = newValue
} else {
this.billing_data = this.shipping_data
}
},
},
}
})
</script>
</body>
</html>
【问题讨论】:
-
只是提到你在 copy_checkbox 中有错字:)
标签: javascript html vue.js nuxt.js