简体   繁体   English

在VueJS中将输入框单向绑定

[英]Bind an inputbox one-way to another in VueJS

Is there in VueJS a way to bind an inputbox to another inputbox, but only one way? VueJS中是否有一种将输入框绑定到另一个输入框的方法,但是只有一种方法?

I want to make a copy of box1 to box2 while typing. 我想在输入时将box1复制到box2。 But when I start editing box2 I want nothing to happen. 但是,当我开始编辑box2时,我什么也不想发生。 (Box2 also has a binding to another field with VueJS) (Box2还使用VueJS绑定到另一个字段)

Seems that my existing jQuery on(click) handler is overruled by Vue... 似乎我现有的jQuery on(click)处理程序已被Vue否决...

Bind the first box to a v-model, then bind the second boxes "value" attribute the box1's model. 将第一个框绑定到v模型,然后将第二个框的“值”属性绑定到box1的模型。 You can give box2 its own model and it should work as well. 您可以给box2自己的模型,它也应该工作。

<div id="app">
    <input type="text" v-model="box1">
    <input type="text" v-model="box2" :value="box1">
</div>

 vm = new Vue({ el: '#app', data: { box1: '', box2: '' } }) 

Got it :) Just add a method for this feature. 明白了:)只需为此功能添加一个方法。

<input v-model="productName" v-on:keyup="updateSKU" type="text">

new Vue({
  el: '#app',
  data : {
    productName : '',
    productSku : ''
  },
  methods : {
    updateSKU : function() {
      this.productSku = this.productName.toUpperCase();
    }
  }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM