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("以達(dá)到上限")
}
},
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)添加:每點(diǎn)擊一次就往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>到此這篇關(guān)于Vue3單擊新增添加新的input的文章就介紹到這了,更多相關(guān)Vue3新增添加新的input內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue中前端如何實(shí)現(xiàn)pdf預(yù)覽功能(含vue-pdf插件用法)
這篇文章主要給大家介紹了vue中前端如何實(shí)現(xiàn)pdf預(yù)覽功能的相關(guān)資料,文中包含vue-pdf插件用法,在前端開發(fā)中,很多時候我們需要進(jìn)行pdf文件的預(yù)覽操作,需要的朋友可以參考下2023-07-07
Vue.js 時間轉(zhuǎn)換代碼及時間戳轉(zhuǎn)時間字符串
這篇文章主要介紹了Vue.js 時間轉(zhuǎn)換代碼及時間戳轉(zhuǎn)時間字符串,需要的朋友可以參考下2018-10-10
詳解vue-router 動態(tài)路由下子頁面多頁共活的解決方案
這篇文章主要介紹了vue-router 動態(tài)路由下子頁面多頁共活的解決方案,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
Vue使用electron轉(zhuǎn)換項(xiàng)目成桌面應(yīng)用方法介紹
Electron也可以快速地將你的網(wǎng)站打包成一個原生應(yīng)用發(fā)布,下面這篇文章主要給大家介紹了關(guān)于Vue和React中快速使用Electron的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-11-11
VUE解決 v-html不能觸發(fā)點(diǎn)擊事件的問題
今天小編就為大家分享一篇VUE解決 v-html不能觸發(fā)點(diǎn)擊事件的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-10-10

