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動(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)文章
vue項(xiàng)目如何實(shí)現(xiàn)Echarts在label中獲取點(diǎn)擊事件
這篇文章主要介紹了vue項(xiàng)目如何實(shí)現(xiàn)Echarts在label中獲取點(diǎn)擊事件,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10VUE+Express+MongoDB前后端分離實(shí)現(xiàn)一個(gè)便簽墻
這篇文章主要介紹了VUE+Express+MongoDB前后端分離實(shí)現(xiàn)一個(gè)便簽墻,需要的朋友可以參考下2021-04-04Vue輸入框?qū)崟r(shí)驗(yàn)證IP地址合法性并在下方進(jìn)行提示功能實(shí)現(xiàn)
在Vue組件中的IP地址輸入框定義一個(gè)checkIpAddress方法,該方法使用正則表達(dá)式對(duì)傳入的IP地址進(jìn)行驗(yàn)證,這篇文章主要介紹了Vue輸入框?qū)崟r(shí)驗(yàn)證IP地址合法性并在下方進(jìn)行提示,需要的朋友可以參考下2024-06-06vue中ref標(biāo)簽屬性和$ref的關(guān)系解讀
這篇文章主要介紹了vue中ref標(biāo)簽屬性和$ref的關(guān)系,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07vue 和vue-touch 實(shí)現(xiàn)移動(dòng)端左右導(dǎo)航效果(仿京東移動(dòng)站導(dǎo)航)
這篇文章主要介紹了vue 和vue-touch 實(shí)現(xiàn)移動(dòng)端左右導(dǎo)航效果(仿京東移動(dòng)站導(dǎo)航),需要的朋友可以參考下2017-04-04