vue實現(xiàn)表單錄入小案例
更新時間:2019年09月27日 11:06:47 作者:小羽向前跑
這篇文章主要為大家詳細(xì)介紹了vue實現(xiàn)表單錄入小案例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了vue實現(xiàn)表單錄入的具體代碼,供大家參考,具體內(nèi)容如下
最終效果:

代碼:
<template>
<div id="app">
<!--第一部分-->
<fieldset>
<legend>學(xué)生錄入系統(tǒng)</legend>
<div>
<span>姓名:</span>
<input type="text" placeholder="請輸入姓名" v-model="newStudent.name">
</div>
<div>
<span>年齡:</span>
<input type="text" placeholder="請輸入年齡" v-model="newStudent.age">
</div>
<div>
<span>性別:</span>
<select v-model="newStudent.sex">
<option value="男">男</option>
<option value="女">女</option>
</select>
</div>
<div>
<span>手機:</span>
<input type="text" placeholder="請輸入手機號碼" v-model="newStudent.phone">
</div>
<button @click="createNewStudent()">創(chuàng)建新用戶</button>
</fieldset>
<!--第二部分-->
<table>
<thead>
<tr>
<td>姓名</td>
<td>性別</td>
<td>年齡</td>
<td>手機</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('請輸入正確的年齡');
return;
}
// 手機號碼
if(this.newStudent.phone === ''){
alert('手機號碼不正確');
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>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- Vue axios 中提交表單數(shù)據(jù)(含上傳文件)
- Vue表單驗證插件Vue Validator使用方法詳解
- Vue+ElementUI實現(xiàn)表單動態(tài)渲染、可視化配置的方法
- Vue ElementUI之Form表單驗證遇到的問題
- Vue form 表單提交+ajax異步請求+分頁效果
- vue表單驗證你真的會了嗎?vue表單驗證(form)validate
- 使用Vue動態(tài)生成form表單的實例代碼
- Vue2.0表單校驗組件vee-validate的使用詳解
- vue使用Element組件時v-for循環(huán)里的表單項驗證方法
- vue2 中如何實現(xiàn)動態(tài)表單增刪改查實例
相關(guān)文章
vue實現(xiàn)點擊關(guān)注后及時更新列表功能
這篇文章主要介紹了vue實現(xiàn)點擊關(guān)注后及時更新列表功能,本文通過實例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2018-06-06
vue?長列表數(shù)據(jù)刷新的實現(xiàn)及思考
這篇文章主要為大家介紹了vue?長列表數(shù)據(jù)刷新的實現(xiàn)及思考,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04
vue的watch監(jiān)聽器取消的方法小結(jié)
在 Vue 中,watch 監(jiān)聽器是可以取消的,取消監(jiān)聽器的方式取決于你是如何使用 watch 的,所以本文給大家餓介紹了vue的watch監(jiān)聽器取消的方法,并通過代碼示例講解的非常詳細(xì),需要的朋友可以參考下2025-02-02
Vue實現(xiàn)docx/xlsx/pdf等類型文件預(yù)覽功能
這篇文章主要為大家詳細(xì)介紹了如何溧陽Vue實現(xiàn)docx/xlsx/pdf等類型文件預(yù)覽功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-02-02
Vue項目中數(shù)據(jù)的深度監(jiān)聽或?qū)ο髮傩缘谋O(jiān)聽實例
這篇文章主要介紹了Vue項目中數(shù)據(jù)的深度監(jiān)聽或?qū)ο髮傩缘谋O(jiān)聽實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07
vue+VeeValidate 校驗范圍實例詳解(部分校驗,全部校驗)
validate()可以指定校驗范圍內(nèi),或者是全局的 字段。而validateAll()只能校驗全局。這篇文章主要介紹了vue+VeeValidate 校驗范圍(部分校驗,全部校驗) ,需要的朋友可以參考下2018-10-10
Vue 使用Props屬性實現(xiàn)父子組件的動態(tài)傳值詳解
今天小編就為大家分享一篇Vue 使用Props屬性實現(xiàn)父子組件的動態(tài)傳值詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-11-11

