+
50
-

vue v-model如何动态绑定变量?

vue v-model如何动态绑定变量?

编入多个input输入,每个input根据index v-model为input_index

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">

    <script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/vue.2.2.min.js"></script>
</head>
<body>
    <div id="app">
        <div v-for="(subitem,j) in inputlist">
            <input v-model="inputv" :value="j+1" type="text" :id="'d'+j" name="opt" />
        </div>
    </div>
    <script>
        new Vue({
            el: '#app',
            data: {
                inputv: "",

                inputlist: [1, 2, 3],

            },

        });

    </script>
</body>
</html>

如何实现不同的input自动绑定不同的-model

高手回复

+
0
-

这样就行了

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">

<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/vue.2.2.min.js"></script>
</head>
<body>
<div id="app">
<div v-for="(subitem,j) in inputlist">
<input v-model="inputdata['line_'+j]" type="text" />
</div>
<button @click="see">
查看
</button>
</div>
<script>
new Vue({
el: '#app',
data: {
inputv: "",
inputdata: {
line_0: '', line_1: '', line_2: ''
},

inputlist: [1, 2, 3],

},
methods: {
see() {
console.log(this.inputdata);
}
}

});

</script>
</body>
</html>


by ppzhuzhu
我知道答案,我要回答