Vue3單擊新增添加新的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("以達到上限")
}
},
inp(){
console.log(this.items)
}
}
}
</script>PS:Vue動態(tài)綁定、添加input
這個過程用到了Vue+element-ui
(1)首先給el-input加上v-for循環(huán)一個數(shù)據(jù),并且v-model綁定這個數(shù)據(jù)中的屬性,這樣就可以在頁面中展示所有的input框了,
(2)動態(tài)綁定:先模擬一個傳過來或者是請求到的數(shù)據(jù),循環(huán)遍歷這個數(shù)據(jù),并把每個數(shù)據(jù)中的屬性賦值給之前el-input循環(huán)的那個數(shù)據(jù)中的屬性,這里推薦for-of循環(huán)。
(3)動態(tài)添加:每點擊一次就往el-input循環(huán)的那個數(shù)據(jù)中添加新的屬性
<template>
? <div class="input_test">
? ? <el-input
? ? ? placeholder="請輸入內(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"],//模擬一個傳過來或者是請求到的數(shù)據(jù)
? ? ? modules: [
? ? ? ? {
? ? ? ? ? text: "text",
? ? ? ? },
? ? ? ],
? ? };
? },
? methods: {
? ? add() {//動態(tài)添加input框
? ? ? this.modules.push({ text: "text" });
? ? },
? },
? watch: {},
? computed: {},
? components: {},
? created() {},
? mounted() {//動態(tài)綁定input框
? ? for (const iterator of this.inputList) {
? ? ? this.modules.push({ text: iterator });
? ? }
? },
};
</script>
?
<style lang="scss" scoped></style>到此這篇關于Vue3單擊新增添加新的input的文章就介紹到這了,更多相關Vue3新增添加新的input內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
vue項目如何實現(xiàn)Echarts在label中獲取點擊事件
這篇文章主要介紹了vue項目如何實現(xiàn)Echarts在label中獲取點擊事件,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10
VUE+Express+MongoDB前后端分離實現(xiàn)一個便簽墻
這篇文章主要介紹了VUE+Express+MongoDB前后端分離實現(xiàn)一個便簽墻,需要的朋友可以參考下2021-04-04
Vue輸入框?qū)崟r驗證IP地址合法性并在下方進行提示功能實現(xiàn)
在Vue組件中的IP地址輸入框定義一個checkIpAddress方法,該方法使用正則表達式對傳入的IP地址進行驗證,這篇文章主要介紹了Vue輸入框?qū)崟r驗證IP地址合法性并在下方進行提示,需要的朋友可以參考下2024-06-06
vue 和vue-touch 實現(xiàn)移動端左右導航效果(仿京東移動站導航)
這篇文章主要介紹了vue 和vue-touch 實現(xiàn)移動端左右導航效果(仿京東移動站導航),需要的朋友可以參考下2017-04-04

