vue實(shí)現(xiàn)表單錄入小案例
本文實(shí)例為大家分享了vue實(shí)現(xiàn)表單錄入的具體代碼,供大家參考,具體內(nèi)容如下
最終效果:

代碼:
<template>
<div id="app">
<!--第一部分-->
<fieldset>
<legend>學(xué)生錄入系統(tǒng)</legend>
<div>
<span>姓名:</span>
<input type="text" placeholder="請(qǐng)輸入姓名" v-model="newStudent.name">
</div>
<div>
<span>年齡:</span>
<input type="text" placeholder="請(qǐng)輸入年齡" v-model="newStudent.age">
</div>
<div>
<span>性別:</span>
<select v-model="newStudent.sex">
<option value="男">男</option>
<option value="女">女</option>
</select>
</div>
<div>
<span>手機(jī):</span>
<input type="text" placeholder="請(qǐng)輸入手機(jī)號(hào)碼" v-model="newStudent.phone">
</div>
<button @click="createNewStudent()">創(chuàng)建新用戶</button>
</fieldset>
<!--第二部分-->
<table>
<thead>
<tr>
<td>姓名</td>
<td>性別</td>
<td>年齡</td>
<td>手機(jī)</td>
<td>刪除</td>
</tr>
</thead>
<tbody>
<tr v-for="(p, index) in persons">
<td>{{p.name}}</td>
<td>{{p.sex}}</td>
<td>{{p.age}}</td>
<td>{{p.phone}}</td>
<td>
<button @click="deleteStudentMsg(index)">刪除</button>
</td>
</tr>
</tbody>
</table>
</div>
</template>
<script>
export default {
name: "todolist2",
data(){
return{
persons: [
{name: '張三', age: 20, sex: '男', phone: '18932323232'},
{name: '李四', age: 30, sex: '男', phone: '18921212122'},
{name: '王五', age: 20, sex: '男', phone: '18932223232'},
{name: '趙六', age: 25, sex: '女', phone: '18932322232'},
],
newStudent: {name: '', age: 0, sex: '男', phone: ''}
}
},
methods: {
// 創(chuàng)建一條新紀(jì)錄
createNewStudent(){
// 姓名不能為空
if(this.newStudent.name === ''){
alert('姓名不能為空');
return;
}
// 年齡不能小于0
if(this.newStudent.age <= 0){
alert('請(qǐng)輸入正確的年齡');
return;
}
// 手機(jī)號(hào)碼
if(this.newStudent.phone === ''){
alert('手機(jī)號(hào)碼不正確');
return;
}
// 往數(shù)組中添加一條新紀(jì)錄
this.persons.unshift(this.newStudent);
// 清空數(shù)據(jù)
this.newStudent = {name: '', age: 0, sex: '男', phone: ''}
},
// 刪除一條學(xué)生紀(jì)錄
deleteStudentMsg(index){
this.persons.splice(index,1);
}
},
}
</script>
<style scoped>
#app{
margin: 50px auto;
width: 600px;
}
fieldset{
border: 1px solid orangered;
margin-bottom: 20px;
}
fieldset input{
width: 200px;
height: 30px;
margin: 10px 0;
}
table{
width: 600px;
border: 2px solid orangered;
text-align: center;
}
thead{
background-color: orangered;
}
</style>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Vue axios 中提交表單數(shù)據(jù)(含上傳文件)
- Vue表單驗(yàn)證插件Vue Validator使用方法詳解
- Vue+ElementUI實(shí)現(xiàn)表單動(dòng)態(tài)渲染、可視化配置的方法
- Vue ElementUI之Form表單驗(yàn)證遇到的問(wèn)題
- Vue form 表單提交+ajax異步請(qǐng)求+分頁(yè)效果
- vue表單驗(yàn)證你真的會(huì)了嗎?vue表單驗(yàn)證(form)validate
- 使用Vue動(dòng)態(tài)生成form表單的實(shí)例代碼
- Vue2.0表單校驗(yàn)組件vee-validate的使用詳解
- vue使用Element組件時(shí)v-for循環(huán)里的表單項(xiàng)驗(yàn)證方法
- vue2 中如何實(shí)現(xiàn)動(dòng)態(tài)表單增刪改查實(shí)例
相關(guān)文章
vue實(shí)現(xiàn)點(diǎn)擊關(guān)注后及時(shí)更新列表功能
這篇文章主要介紹了vue實(shí)現(xiàn)點(diǎn)擊關(guān)注后及時(shí)更新列表功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-06-06
vue?長(zhǎng)列表數(shù)據(jù)刷新的實(shí)現(xiàn)及思考
這篇文章主要為大家介紹了vue?長(zhǎng)列表數(shù)據(jù)刷新的實(shí)現(xiàn)及思考,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04
輕松理解vue的雙向數(shù)據(jù)綁定問(wèn)題
Vue是當(dāng)前很火的一款MVVM的輕量級(jí)框架,它是以數(shù)據(jù)驅(qū)動(dòng)和組件化的思想構(gòu)建的。因?yàn)樗峁┝撕?jiǎn)潔易于理解的api,使得我們很容易上手。這篇文章給大家介紹了vue雙向數(shù)據(jù)綁定問(wèn)題,感興趣的朋友一起看看吧2017-10-10
vue的watch監(jiān)聽(tīng)器取消的方法小結(jié)
在 Vue 中,watch 監(jiān)聽(tīng)器是可以取消的,取消監(jiān)聽(tīng)器的方式取決于你是如何使用 watch 的,所以本文給大家餓介紹了vue的watch監(jiān)聽(tīng)器取消的方法,并通過(guò)代碼示例講解的非常詳細(xì),需要的朋友可以參考下2025-02-02
Vue實(shí)現(xiàn)docx/xlsx/pdf等類型文件預(yù)覽功能
這篇文章主要為大家詳細(xì)介紹了如何溧陽(yáng)Vue實(shí)現(xiàn)docx/xlsx/pdf等類型文件預(yù)覽功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-02-02
Vue項(xiàng)目中數(shù)據(jù)的深度監(jiān)聽(tīng)或?qū)ο髮傩缘谋O(jiān)聽(tīng)實(shí)例
這篇文章主要介紹了Vue項(xiàng)目中數(shù)據(jù)的深度監(jiān)聽(tīng)或?qū)ο髮傩缘谋O(jiān)聽(tīng)實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-07-07
vue+VeeValidate 校驗(yàn)范圍實(shí)例詳解(部分校驗(yàn),全部校驗(yàn))
validate()可以指定校驗(yàn)范圍內(nèi),或者是全局的 字段。而validateAll()只能校驗(yàn)全局。這篇文章主要介紹了vue+VeeValidate 校驗(yàn)范圍(部分校驗(yàn),全部校驗(yàn)) ,需要的朋友可以參考下2018-10-10
Vue 使用Props屬性實(shí)現(xiàn)父子組件的動(dòng)態(tài)傳值詳解
今天小編就為大家分享一篇Vue 使用Props屬性實(shí)現(xiàn)父子組件的動(dòng)態(tài)傳值詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11

