Vue动态绑定、添加input

这个过程用到了Vue+element-ui

(1)首先给el-input加上v-for循环一个数据,并且v-model绑定这个数据中的属性,这样就可以在页面中展示所有的input框了,

(2)动态绑定:先模拟一个传过来或者是请求到的数据,循环遍历这个数据,并把每个数据中的属性赋值给之前el-input循环的那个数据中的属性,这里推荐for-of循环。

(3)动态添加:每点击一次就往el-input循环的那个数据中添加新的属性

具体操作如下: 

<template>
  <div class="input_test">
    <el-input
      placeholder="请输入内容"
      v-for="(item, index) in modules"
      :key="index"
      v-model="item.text"
    ></el-input>
    <el-button type="success" @click="add">新增</el-button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      inputList: ["inputOne", "inputTwo", "inputThree"],//模拟一个传过来或者是请求到的数据
      modules: [
        {
          text: "text",
        },
      ],
    };
  },
  methods: {
    add() {//动态添加input框
      this.modules.push({ text: "text" });
    },
  },
  watch: {},
  computed: {},
  components: {},
  created() {},
  mounted() {//动态绑定input框
    for (const iterator of this.inputList) {
      this.modules.push({ text: iterator });
    }
  },
};
</script>

<style lang="scss" scoped></style>

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值