vue组件表单数据回显验证及提交的实例代码


Posted in Javascript onAugust 30, 2018

最近项目需要到vue开发单页面,所以就研究一下表单数据的回显,验证及提交如何用vue组件的方式实现。

vue组件表单数据回显验证及提交的实例代码

vue组件表单数据回显验证及提交的实例代码

代码如下:

<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>设备名称:</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">点击保存</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("输入设备名称");
  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("####设备名称####");
 console.log(this.shebeiType);

 console.log("####住户大小####");
 console.log(this.userDxselected);

 console.log("####用户留言####");
 console.log(this.userword);
 },
 //住户类型改变数据
 changeZh(index){
 this.zhuhuType.forEach(function(value, index){
  value.isChecked = false;
 });
 this.zhuhuType[index].isChecked = true;
 },
 //设备选择改变数据
 changeSb(index){
 console.log(index);
 this.shebeiType[index].isChecked = !this.shebeiType[index].isChecked;
 }
 },
 created () {
 console.log("############");
 //用户名称
 this.formStatus.username = "用户名称返回的内容";

 //循环住户类型
 this.zhuhuType = [{
 name: '小型住户',
 id: '12',
 isChecked: false
 },{
 name: '中型住户',
 id: '43',
 isChecked: false
 },{
 name: '大型住户',
 id: '72',
 isChecked: true
 },{
 name: '超大型住户',
 name: '设备6',
 id: '25',
 isChecked: false
 }];

 //循环设备名称
 this.shebeiType = [{
 name: '设备1',
 id: '22',
 isChecked: true
 },{
 name: '设备2',
 id: '23',
 isChecked: false
 },{
 name: '设备3',
 id: '52',
 isChecked: true
 },{
 name: '设备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 = "用户留言返回的内容";
 // 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>
Javascript 相关文章推荐
JQuery 学习笔记 选择器之一
Jul 23 Javascript
JQuery 动画卷页 返回顶部 动画特效(兼容Chrome)
Feb 15 Javascript
13个绚丽的Jquery 界面设计网站推荐
Sep 28 Javascript
jQuery队列控制方法详解queue()/dequeue()/clearQueue()
Dec 02 Javascript
巧用replace将文字表情替换为图片
Apr 17 Javascript
JavaScript限定图片显示大小的方法
Mar 11 Javascript
jQuery Mobile动态刷新页面样式的实现方法
May 28 Javascript
jQuery中 $ 符号的冲突问题及解决方案
Nov 04 Javascript
JavaScript调试的多个必备小Tips
Jan 15 Javascript
vue实现微信分享功能
Nov 28 Javascript
vue中实现上传文件给后台实例详解
Aug 22 Javascript
jQuery操作动画完整实例分析
Jan 10 jQuery
原生JS实现的简单小钟表功能示例
Aug 30 #Javascript
使用Node搭建reactSSR服务端渲染架构
Aug 30 #Javascript
webpack4+Vue搭建自己的Vue-cli项目过程分享
Aug 29 #Javascript
JS实现的input选择图片本地预览功能示例
Aug 29 #Javascript
简述vue状态管理模式之vuex
Aug 29 #Javascript
jQuery实现的简单手风琴效果示例
Aug 29 #jQuery
angularjs实现对表单输入改变的监控(ng-change和watch两种方式)
Aug 29 #Javascript
You might like
摩卡咖啡
2021/03/03 咖啡文化
php数据结构与算法(PHP描述) 查找与二分法查找
2012/06/21 PHP
Linux下实现PHP多进程的方法分享
2012/08/16 PHP
PHP中遇到的时区问题解决方法
2015/07/23 PHP
PHP示例演示发送邮件给某个邮箱
2019/04/03 PHP
javascript替换已有元素replaceChild()使用介绍
2014/04/03 Javascript
jquery 显示*天*时*分*秒实现时间计时器
2014/05/07 Javascript
AngularJs 60分钟入门基础教程
2016/04/03 Javascript
Vue-Router实现页面正在加载特效方法示例
2017/02/12 Javascript
jQuery.ajax向后台传递数组问题的解决方法
2017/05/12 jQuery
js数组方法reduce经典用法代码分享
2018/01/07 Javascript
使用 vue.js 构建大型单页应用
2018/02/10 Javascript
在微信小程序里使用watch和computed的方法
2018/08/02 Javascript
axios取消请求的实践记录分享
2018/09/26 Javascript
浅谈Vue CLI 3结合Lerna进行UI框架设计
2019/04/14 Javascript
create-react-app中添加less支持的实现
2019/11/15 Javascript
Vue使用Three.js加载glTF模型的方法详解
2020/06/14 Javascript
[01:23:45]DOTA2-DPC中国联赛 正赛 CDEC vs Dragon BO3 第一场 1月22日
2021/03/11 DOTA
一个基于flask的web应用诞生 flask和mysql相连(4)
2017/04/11 Python
Python中XlsxWriter模块简介与用法分析
2018/04/24 Python
Python学习_几种存取xls/xlsx文件的方法总结
2018/05/03 Python
Python argparse模块应用实例解析
2019/11/15 Python
在Python中使用MySQL--PyMySQL的基本使用方法
2019/11/19 Python
python pygame实现滚动横版射击游戏城市之战
2019/11/25 Python
Python函数参数分类原理详解
2020/05/28 Python
HTML5中meta属性的使用方法
2016/02/29 HTML / CSS
海信商城:海信电视、科龙空调、容声冰箱官方专卖
2017/02/07 全球购物
《邮票齿孔的故事》教学反思
2014/02/22 职场文书
海飞丝的广告词
2014/03/20 职场文书
三方合作协议书范本
2014/04/18 职场文书
环境整治工作方案
2014/05/18 职场文书
班主任寄语2015
2015/02/26 职场文书
2016年教师党员公开承诺书
2016/03/24 职场文书
python中pandas.read_csv()函数的深入讲解
2021/03/29 Python
「月刊Action」2022年5月号封面公开
2022/03/21 日漫
MySql中的json_extract函数处理json字段详情
2022/06/05 MySQL