Vue表單數據的自動收集

<body>
   <div id="app">
       <form action="/sss" @submit.prevent = "handleSubmit">
           <span>用戶名:</span>
           <input type="text" v-model ="username" >          
           <span>密碼:</span>
           <input type="password" v-model = "password">
           
           <span>性別:</span>
           <input type="radio" value="男" id="male" v-model = "sex">
           <label for="male">男</label>
           <input type="radio" value="女" id="famale" v-model = "sex">
           <label for="famale">女</label>
           
           <span>愛好:</span>
           <input type="checkbox" value="basketball" id="basketball" v-model = "likes">
           <label for="basketball">籃球</label>
           <input type="checkbox" value="football" id="football" v-model = "likes">
           <label for="football">足球</label>
           <input type="checkbox" value="pingpang" id="pingpang" v-model = "likes">
           <label for="pingpang">乒乓球</label>
           <input type="checkbox" value="golf" id="golf" v-model = "likes">
           <label for="golf">高爾夫球</label>
           
           <span>城市:</span>
           <select v-model="cityId">
               <option value="">為選擇</option>
               <option :value="city.id" v-for= "(city,index) in allcitys " :key="index">{{city.name}}</option>
           </select>
           
           <span>簽名:</span>
           <textarea  rows="10" v-model="desc"></textarea>         
           <input type="submit" value="注冊">
       </form>       
   </div>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <script>
        new Vue({
            el:"#app",
            data:{
                username :"",
                password:"",
                sex:"男",
                likes:["golf"],
                allcitys:[{id:1,name:"bj"},{id:2,name:"sh"},{id:3,name:"gd"},{id:4,name:"sx"}],
                cityId:'1',
                desc:'',
            },
            methods: {
                handleSubmit(){  
              console.log(this.username,this.password,this.sex,this.likes,this.cityId,this.desc);                  
                }
            },
        })
    </script>
</body>

通過v-model的數據雙向綁定,

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容