• VUE+Layui 输入框、下拉选择框、复选框、开关、文本域 数据回显


    一、说明

    用了Layui的界面,加上vue的指令:v-modelv-bindv-for

    • 1、注意:开关按钮如果是关闭状态:status字段是不会在表单提交字段中的,打开了就是status:1,所以表单编辑时如果开关由打开变成关闭了,我们的表单数据field需加上status判断,如下:
    field.status = field.status ? field.status : 0;
    
    • 1
    • 2、点开类型下拉框会发现默认选到最后一个,lay-verify="required"校验失效;所以我们需要设置type的默认值为’’

    二、效果

    • 1、默认页面
      在这里插入图片描述

    • 2、点击 [ 回显 ] 页面

    在这里插入图片描述

    • 3、点击 [ 提交 ] 页面

    在这里插入图片描述

    三、代码

    DOCTYPE html>
    <html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>VUE+Layui 输入框、下拉选择框、复选框、开关、文本域 数据回显title>
    head>
    
    <link rel="stylesheet" type="text/css" href="https://www.layuicdn.com/layui/css/layui.css" v="2.5.6" e="layui"/>
    <style>
        .w100 {
            width: 200px;
        }
    style>
    <body>
    <div class="layui-form" id="vue-mount-element" style="padding: 30px 50px 0 0;">
        <div class="layui-form-item">
            <label class="layui-form-label w100">输入框:标题label>
            <div class="layui-input-inline">
                <input type="text" lay-verify="required" name="title" :value="info.title" placeholder="请输入标题"
                       autocomplete="off" class="layui-input">
            div>
        div>
    
        <div class="layui-form-item">
            <label class="layui-form-label w100">下拉单选框:类型label>
            <div class="layui-input-inline">
                <select name="type" lay-verify="required" v-model="info.type">
                    <option value="">请选择类型option>
                    <option v-for="item in types" :value="item.id">{{item.name}}option>
                select>
            div>
        div>
    
        <div class="layui-form-item">
            <label class="layui-form-label w100">复选框:爱好label>
            <div class="layui-input-block">
                <input type="checkbox" v-for="item in hobbyList"
                       :disabled="disableHobbyId.length != 0 && (disableHobbyId.indexOf(parseInt(item.id)) != -1)"
                       name="hobby2[]" :value="item.id" :title="item.name" v-model="info.chooseHobbyId" lay-skin="primary"/>
            div>
        div>
    
        <div class="layui-form-item">
            <label class="layui-form-label w100">开关:状态(默认开启)label>
            <div class="layui-input-inline">
                <input type="checkbox" name="status" value="1" v-model="info.status" lay-skin="switch" lay-text="开启|关闭">
            div>
        div>
    
        <div class="layui-form-item">
            <label class="layui-form-label w100">文本域:描述label>
            <div class="layui-input-inline">
                <textarea name="desc" placeholder="请输入描述" class="layui-textarea">{{info.desc}}textarea>
            div>
        div>
    
        <div class="layui-form-item">
            <label class="layui-form-label w100">
    
            label>
            <div class="layui-input-block">
                <button class="layui-btn layuiadmin-btn-admin" lay-submit>
                    提交
                button>
                <button class="layui-btn layui-btn-warm" id="backButton">
                    回显
                button>
                <button class="layui-btn layui-btn-normal" id="reductionButton">
                    还原
                button>
            div>
        div>
    div>
    body>
    
    <script src="https://www.layuicdn.com/layui/layui.js" v="2.5.6" e="layui.all">script>
    
    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js">script>
    <script>
        //爱好列表选项
        let hobbyList = [
            {id: 1, name: "爱好1"},
            {id: 2, name: "爱好2"},
            {id: 3, name: "爱好3"},
            {id: 4, name: "爱好4"},
            {id: 5, name: "爱好5"},
            {id: 6, name: "爱好6"},
        ]; //爱好列表
        let disableHobbyId = [2, 4]; //不能选中的爱好(禁用的爱好)
    
        //类型
        let types = [
            {id: 1, name: "类型1"},
            {id: 2, name: "类型2"},
            {id: 3, name: "类型3"},
        ];
    
        let info = {}; //后端传过来的值,前端这么写,PHP的话传过来可能是这样[]
        //默认的值:点开类型下拉框会发现默认选到最后一个,lay-verify="required"校验失效;所以我们需要设置type的默认值为'',选择的爱好chooseHobbyId设置为空数组[]
        let defaultInfo = {type: '', status: 1, chooseHobbyId: []}; //默认开启 status: 1,chooseHobbyId:用户选中的爱好
    
        //PHP传过来是[],以数组的方式判断
        /*if (info.length === 0) {
            info = defaultInfo; //添加,给默认值
        }*/
    
        //前端是{},判断对象:将json对象转化为json字符串,再判断该字符串是否为"{}"
        if (JSON.stringify(info) == "{}") {
            info = defaultInfo; //添加,给默认值
        }
    
        //定义回显的数据
        let backInfo = {title: "这是输入的标题", type: 2, status: 0, desc: "这是输入描述的内容", chooseHobbyId: [1, 2]};
    
        let vueObject = new Vue({
            el: '#vue-mount-element', //vue挂载的元素
            data() {
                return {
                    info: info,
                    types: types,
                    hobbyList: hobbyList,
                    disableHobbyId: disableHobbyId
                };
            }
        })
    
        layui.use(['laydate', 'laypage', 'layer', 'table', 'carousel', 'upload', 'element', 'form'], function () {
            var $ = layui.$
                , laydate = layui.laydate //日期
                , laypage = layui.laypage //分页
                , layer = layui.layer //弹层
                , table = layui.table //表格
                , carousel = layui.carousel //轮播
                , upload = layui.upload //上传
                , form = layui.form //表单
                , element = layui.element; //元素操作 等等...
    
            /*layer弹出一个示例*/
            // layer.msg('Hello World');
    
            form.on('submit', function (data) {
                let field = data.field;
                field = JSON.stringify(field);
                layer.alert("表单的内容为:" + field);
            });
    
            //回显操作
            $('#backButton').click(function () {
                vueObject.info = backInfo; //修改vue对象中data中info的值
    
                //增加延时效果,回显数据渲染
                setTimeout(function () {
                    form.render();
                }, 100);
            });
    
            //还原操作
            $('#reductionButton').click(function () {
                vueObject.info = defaultInfo; //修改vue对象中data中info的值
    
                //增加延时效果,回显数据渲染
                setTimeout(function () {
                    form.render();
                }, 100);
            });
    
        });
    script>
    html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169

    四、如果失效了

    • 请自行寻找其他的 线上 cssjs
  • 相关阅读:
    python 2018全国自学考试第5章 第35题,求出完整数了我忽略了。我竟然成功了太好了 排名21297
    Spring Controller内存马
    springboot集成Logback 日志写入数据库
    开源云财务软件,财务软件源码,永久免费财务软件
    公网集群对讲+GPS可视追踪|助力物流行业智能化管理调度
    企业数字化成功转型的关键,从这三方面出发
    【逻辑与计算机设计】数码系统和数字系统 | Digital systems and number systems
    不小心删除了docker/overlay2怎么办?
    Java堆外缓存(一个很有意思的应用)
    OpenAI Sora引领AI跳舞视频新浪潮:字节跳动发布创新舞蹈视频生成框架
  • 原文地址:https://blog.csdn.net/qq_36025814/article/details/125860587