vue組件表單數(shù)據(jù)回顯驗(yàn)證及提交的實(shí)例代碼
更新時(shí)間:2018年08月30日 08:48:59 作者:codingNoob
今天小編就為大家分享一篇vue組件表單數(shù)據(jù)回顯驗(yàn)證及提交的實(shí)例代碼,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
最近項(xiàng)目需要到vue開發(fā)單頁面,所以就研究一下表單數(shù)據(jù)的回顯,驗(yàn)證及提交如何用vue組件的方式實(shí)現(xiàn)。


代碼如下:
<template>
<div class="index">
<!--header-bar></header-bar-->
<div style="margin:20px;">
<div class="item">
<p>住戶名稱:</p>
<p>
<input type="text" value="username" v-model="formStatus.username" placeholder="輸入名稱">
</p>
</div>
<div class="item">
<p>住戶類型:</p>
<p>
<label v-for="(item, index) in zhuhuType">
<span>{{item.name}}</span>
<input type="radio" name = "zhuhutype" :value="item.id" :checked="item.isChecked" @click="changeZh(index)" v-model="formStatus.zhuhuType">
</label>
</p>
</div>
<div class="item">
<p>設(shè)備名稱:</p>
<p>
<label v-for="(item, index) in shebeiType">
<span>{{item.name}}</span>
<input type="checkbox" :value="item.id" :checked="item.isChecked" @click="changeSb(index)" v-model="formStatus.shebeiType">
</label>
</p>
</div>
<div class="item">
<p>住戶大小:</p>
<p>
<select v-model="formStatus.zhuhudaxiao">
<option value="0">請選擇</option>
<option v-for="option in zhuhudaxiao" v-model="zhuhudaxiao" :id = "option.id" :value="option.value" >{{option.name}}</option>
</select>
</p>
</div>
<div class="item">
<p>住戶留言:</p>
<p>
<textarea value="userword" v-model="formStatus.userword"></textarea>
</p>
</div>
</div>
<p style="margin:20px 0;"><button @click="save">點(diǎn)擊保存</button></p>
</div>
</template>
<script>
import Vue from 'vue'
import axios from 'axios';
import ElementUI from 'element-ui'
import URL from '../utils/Tools/URL.js'
import 'element-ui/lib/theme-chalk/index.css'
Vue.use(ElementUI)
import headerBar from '../modules/headerBar.vue';
export default {
name: 'index',
data (){
return {
zhuhuType: [],
shebeiType: [],
zhuhudaxiao: [],
//綁定改變后的表單值用于提交
formStatus: {
username: "",
zhuhuType: 43,
shebeiType: [52, 23],
zhuhudaxiao: "",
userword: ""
}
}
},
components: { headerBar },
methods: {
getPage (currentPage){
console.log(currentPage);
// this.$http.get("http://192.168.1.200/test.php").then(res=>{
// console.log(res.data);
// });
},
handleEdit(index, row) {
console.log(index, row);
},
handleDelete(index, row) {
console.log(index, row);
},
save(){
if(this.formStatus.username == ""){
alert("輸入名稱");
return false;
}
console.log(typeof(this.formStatus.zhuhuType));
if(typeof(parseInt(this.formStatus.zhuhuType)) != "number"){
alert("輸入住戶類型");
return false;
}
if(this.formStatus.shebeiType.length == 0 ){
alert("輸入設(shè)備名稱");
return false;
}
if(this.formStatus.zhuhudaxiao == 0){
alert("選擇住戶大小");
return false;
}
if(this.formStatus.userword == ""){
alert("輸入用戶留言");
return false;
}
console.log(this.formStatus);
console.log("####用戶名稱####");
console.log(this.formStatus.username);
console.log("####住戶類型####");
console.log(this.zhuhuType);
console.log("####設(shè)備名稱####");
console.log(this.shebeiType);
console.log("####住戶大小####");
console.log(this.userDxselected);
console.log("####用戶留言####");
console.log(this.userword);
},
//住戶類型改變數(shù)據(jù)
changeZh(index){
this.zhuhuType.forEach(function(value, index){
value.isChecked = false;
});
this.zhuhuType[index].isChecked = true;
},
//設(shè)備選擇改變數(shù)據(jù)
changeSb(index){
console.log(index);
this.shebeiType[index].isChecked = !this.shebeiType[index].isChecked;
}
},
created () {
console.log("############");
//用戶名稱
this.formStatus.username = "用戶名稱返回的內(nèi)容";
//循環(huán)住戶類型
this.zhuhuType = [{
name: '小型住戶',
id: '12',
isChecked: false
},{
name: '中型住戶',
id: '43',
isChecked: false
},{
name: '大型住戶',
id: '72',
isChecked: true
},{
name: '超大型住戶',
name: '設(shè)備6',
id: '25',
isChecked: false
}];
//循環(huán)設(shè)備名稱
this.shebeiType = [{
name: '設(shè)備1',
id: '22',
isChecked: true
},{
name: '設(shè)備2',
id: '23',
isChecked: false
},{
name: '設(shè)備3',
id: '52',
isChecked: true
},{
name: '設(shè)備6',
id: '65',
isChecked: false
}];
//住戶大小
this.zhuhudaxiao = [{
name: "100平米",
id: 1,
value: 1
},{
name: "80平米",
id: 2,
value: 2
},{
name: "70平米",
id: 3,
value: 3
}];
//住戶大小
this.formStatus.zhuhudaxiao = 2;
//用戶名稱
this.formStatus.userword = "用戶留言返回的內(nèi)容";
// axios.get("/test.php").then(res=>{
// this.table = this.table.concat(res.data.data);
// console.log(res.data);
// });
}
}
</script>
<style>
select{height:40px;width:100px;}
.el-button--mini, .el-button--mini.is-round{}
*{margin:0;padding:0;font-family:"微軟雅黑";}
button{height:40px;width:100px;margin-left:20px;}
.item p{padding:10px 0;}
.table, .page{
width:900px;
height:auto;
margin:20px auto;
}
label{
padding:10px 20px;
margin:10px;
border:1px solid #eee;
}
body{padding-top:80px;}
.header{
position:fixed;
top:0;
width:100%;
background:#eee;
}
.header ul{
height:80px;
width:1000px;
}
.header ul li{
float:left;
width:200px;
font-size:14px;
line-height:80px;
text-align:Center;
}
.header ul li a{
display:block;
}
</style>
您可能感興趣的文章:
- vue使用Element組件時(shí)v-for循環(huán)里的表單項(xiàng)驗(yàn)證方法
- Vue2.0表單校驗(yàn)組件vee-validate的使用詳解
- Vue表單類的父子組件數(shù)據(jù)傳遞示例
- 詳解vue表單驗(yàn)證組件 v-verify-plugin
- Vue form表單動態(tài)添加組件實(shí)戰(zhàn)案例
- vue動態(tài)綁定組件子父組件多表單驗(yàn)證功能的實(shí)現(xiàn)代碼
- 使用form-create動態(tài)生成vue自定義組件和嵌套表單組件
- vue2.0數(shù)據(jù)雙向綁定與表單bootstrap+vue組件
- 利用Vue v-model實(shí)現(xiàn)一個自定義的表單組件
- vue懸浮表單復(fù)合組件開發(fā)詳解
相關(guān)文章
詳解vue-cli 構(gòu)建Vue項(xiàng)目遇到的坑
本篇文章主要介紹了詳解vue-cli 構(gòu)建Vue項(xiàng)目遇到的坑,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-08-08
webpack 3 + Vue2 使用dotenv配置多環(huán)境的步驟
這篇文章主要介紹了webpack 3 + Vue2 使用dotenv配置多環(huán)境,env文件在配置文件都可以用, vue頁面用的時(shí)候需要在 webpack.base.conf.js 重新配置,需要的朋友可以參考下2023-11-11
vue3如何通過provide和inject實(shí)現(xiàn)多層級組件通信
這篇文章主要介紹了vue3如何通過provide和inject實(shí)現(xiàn)多層級組件通信,本文通過實(shí)例代碼給大家講解的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-11-11
把vue-router和express項(xiàng)目部署到服務(wù)器的方法
下面小編就為大家分享一篇把vue-router和express項(xiàng)目部署到服務(wù)器的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
2018-02-02
解決vue與node模版引擎的渲染標(biāo)記{{}}(雙花括號)沖突問題
這篇文章主要介紹了解決vue與node模版引擎的渲染標(biāo)記{{}}(雙花括號)沖突問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
2020-09-09 
