欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Vue3單擊新增添加新的input的方法

 更新時(shí)間:2023年01月07日 09:55:03   作者:最帥爸爸  
這篇文章主要介紹了Vue3單擊新增添加新的input,本文結(jié)合示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,文中補(bǔ)充介紹了Vue動(dòng)態(tài)綁定、添加input,需要的朋友可以參考下

效果圖:

代碼:

<template>
  <div>
    <input type="text" v-for="(item,i) of items" v-model="items[i]" :key="i" @input="inp"> 
    <button @click="onAdd">添加</button>
  </div>
</template>

<script lang="ts">
export default {
  data() {
    return {
      items: [""]
    }
  },
  methods: {
    onAdd() {
      if(this.items.length<5){
        this.items.push('')
      }else{
        alert("以達(dá)到上限")
      }
    },
    inp(){
      console.log(this.items)
    }
  }
}
</script>

PS:Vue動(dòng)態(tài)綁定、添加input

這個(gè)過程用到了Vue+element-ui

(1)首先給el-input加上v-for循環(huán)一個(gè)數(shù)據(jù),并且v-model綁定這個(gè)數(shù)據(jù)中的屬性,這樣就可以在頁(yè)面中展示所有的input框了,

(2)動(dòng)態(tài)綁定:先模擬一個(gè)傳過來或者是請(qǐng)求到的數(shù)據(jù),循環(huán)遍歷這個(gè)數(shù)據(jù),并把每個(gè)數(shù)據(jù)中的屬性賦值給之前el-input循環(huán)的那個(gè)數(shù)據(jù)中的屬性,這里推薦for-of循環(huán)。

(3)動(dòng)態(tài)添加:每點(diǎn)擊一次就往el-input循環(huán)的那個(gè)數(shù)據(jù)中添加新的屬性

<template>
? <div class="input_test">
? ? <el-input
? ? ? placeholder="請(qǐng)輸入內(nèi)容"
? ? ? 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"],//模擬一個(gè)傳過來或者是請(qǐng)求到的數(shù)據(jù)
? ? ? modules: [
? ? ? ? {
? ? ? ? ? text: "text",
? ? ? ? },
? ? ? ],
? ? };
? },
? methods: {
? ? add() {//動(dòng)態(tài)添加input框
? ? ? this.modules.push({ text: "text" });
? ? },
? },
? watch: {},
? computed: {},
? components: {},
? created() {},
? mounted() {//動(dòng)態(tài)綁定input框
? ? for (const iterator of this.inputList) {
? ? ? this.modules.push({ text: iterator });
? ? }
? },
};
</script>
?
<style lang="scss" scoped></style>

到此這篇關(guān)于Vue3單擊新增添加新的input的文章就介紹到這了,更多相關(guān)Vue3新增添加新的input內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論