简体   繁体   English

VueJS v-model 值到另一个输入

[英]VueJS v-model value in to another input

I have two inputs, first:我有两个输入,首先:

<input v-model="from_amount" id="from_amount" type="text" class="form-control" name="from_amount">

And the second:第二个:

<input id="from_amount" type="text" class="form-control" name="to_amount" value="@{{ from_amount }}">

If i type number in from_amount it should be outputted in to_amount如果我在from_amount中输入数字,它应该在to_amount中输出

Here's my VueJS code:这是我的 VueJS 代码:

var request = new Vue({
    el: '#request-creator',
        data: {
            from_amount: '',
            to_amount: ''
        },
        computed: {
            calculate: function() {
                return (this.from_amount * 750) / 0.00024
            }
        }
})

But seems like it's impossible to do with Vue?但似乎用 Vue 做不到?

You need to use v-bind , to bind computed property to an input field like following:您需要使用v-bind将计算属性绑定到输入字段,如下所示:

<input id="from_amount" type="text" class="form-control" name="to_amount" v-bind:value="calculatedFromAmount">

or in short, you can also write或者简而言之,你也可以写

 ... :value="calculatedFromAmount">

See Working fiddle: http://jsfiddle.net/bvr9754h/见工作小提琴:http: //jsfiddle.net/bvr9754h/

You have to define computed property like following in due component:您必须在适当的组件中定义如下计算属性:

    computed: {
        calculatedFromAmount: function() {
            return (this.from_amount * 750) / 0.00024
        }
    }

it's very possible.很有可能。

Modify your code so that to_amount is the name of the computed property :修改您的代码,使to_amount是计算属性的名称:

var request = new Vue({
el: '#request-creator',
    data: {
        from_amount: '',
    },
    computed: {
        to_amount: function() {
            return (this.from_amount * 750) / 0.00024
        }
    }
})

and the html to :和 html 到:

<input id="from_amount" type="text" class="form-control" name="to_amount" :value="to_amount">

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

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