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 相关文章推荐
node.js学习总结之调式代码的方法
Jun 25 Javascript
常用javascript表单验证汇总
Jul 20 Javascript
js HTML5多媒体影音播放
Oct 17 Javascript
bootstrap模态框示例代码分享
May 17 Javascript
浅谈struts1 &amp; jquery form 文件异步上传
May 25 jQuery
vue-cli如何引入bootstrap工具的方法
Oct 19 Javascript
Vue.js 点击按钮显示/隐藏内容的实例代码
Feb 08 Javascript
对node.js中render和send的用法详解
May 14 Javascript
利用js将ajax获取到的后台数据动态加载至网页中的方法
Aug 08 Javascript
大转盘抽奖小程序版 转盘抽奖网页版
Apr 16 Javascript
vue 验证码界面实现点击后标灰并设置div按钮不可点击状态
Oct 28 Javascript
vue2.0实现列表数据增加和删除
Jun 17 Javascript
原生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
人族 TERRAN 概述
2020/03/14 星际争霸
PHP 内存缓存加速功能memcached安装与用法
2009/09/03 PHP
php实现utf-8和GB2312编码相互转换函数代码
2013/02/07 PHP
PHP中的替代语法简介
2014/08/22 PHP
PHP判断访客是否手机端(移动端浏览器)访问的方法总结【4种方法】
2019/03/27 PHP
PHP 进程池与轮询调度算法实现多任务的示例代码
2019/11/26 PHP
JS实现图片预加载无需等待
2012/12/21 Javascript
基于JS实现checkbox全选功能实例代码
2016/10/31 Javascript
JS中如何实现复选框全选功能
2016/12/19 Javascript
jQuery给表格添加分页效果
2017/03/02 Javascript
React Native 搭建开发环境的方法步骤
2017/10/30 Javascript
微信小程序http连接访问解决方案的示例
2018/11/05 Javascript
解决vuex刷新状态初始化的方法实现
2019/08/15 Javascript
JS使用Chrome浏览器实现调试线上代码
2020/07/23 Javascript
Element Rate 评分的使用方法
2020/07/27 Javascript
在实例中重学JavaScript事件循环
2020/12/03 Javascript
python正则匹配抓取豆瓣电影链接和评论代码分享
2013/12/27 Python
在Python中处理字符串之ljust()方法的使用简介
2015/05/19 Python
12步入门Python中的decorator装饰器使用方法
2016/06/20 Python
python 简单照相机调用系统摄像头实现方法 pygame
2018/08/03 Python
python3中property使用方法详解
2019/04/23 Python
Python generator生成器和yield表达式详解
2019/08/08 Python
深入了解python中元类的相关知识
2019/08/29 Python
python图形用户接口实例详解
2019/12/16 Python
印度首选时尚目的地:Reliance Trends
2018/01/17 全球购物
Kipling意大利官网:世界著名的时尚休闲包袋品牌
2019/06/05 全球购物
迪拜领先运动补剂零售品牌中文站:Sporter商城
2019/08/20 全球购物
医药专业推荐信
2013/11/15 职场文书
运动会入场解说词
2014/02/07 职场文书
工作违纪检讨书
2014/02/17 职场文书
企业年会主持词
2014/03/27 职场文书
校园主题婚礼活动策划方案
2014/09/15 职场文书
民间借贷协议书范本
2014/10/01 职场文书
MongoDB数据库的安装步骤
2021/06/18 MongoDB
解决Mysql报错 Table 'mysql.user' doesn't exist
2022/05/06 MySQL
聊聊CSS粘性定位sticky案例解析
2022/06/01 HTML / CSS