v-for動態添加表單,並且獲取表單中的值


vue是數據驅動視圖,所以要想改變頁面的結構,就要首先改變數據。
要想動態添加一個input表單,每當你點擊的時候,添加一個數據在數組里面。
 handler(mess){
    this.list.push("jo")
 },

要想獲得當前這個表單里面的值,我當初想的是v-model,去綁定。發現不可以。這就尷尬了。
整了一會,在百度上查找了一些資料。
還是沒有對應的案例。可能是我百度的方法是不正確。

最后,還是決定使用原生的js
給表單綁定一個id。在失去焦點的時候,得到這個input框中的值。
 let aa=document.getElementById("demo"+mess).value;
<template>
    <div>
       
     <div v-for="(item,index) in list" :key="index" class="demo">
         <input type="text" :id="`demo${index}`">
         <button @click="handler(index)" @blur="getterValue(index)">添加</button>
     </div>

    </div>
</template>

<script>
    export default {
        data(){
          return{
            list:["12"]
          }
        },
        methods:{
            handler(mess){
                this.list.push("jo")
            },
            getterValue(mess){
                let aa=document.getElementById("demo"+mess).value;
                console.log("得到的值是", aa)
            }
        }
    }
</script>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM