layui时间控件清空之后无法正常使用的问题


当前第2页 返回上一页

html代码

1

2

3

4

5

6

7

8

9

10

11

12

<div class="layui-inline">

<div class="layui-input-inline">

<input type="text" name="start_time" class="layui-input" id="start_time"

placeholder="开始时间(修改时间)">

</div>

</div>

<div class="layui-inline">

<div class="layui-input-inline">

<input type="text" name="end_time" class="layui-input" id="end_time"

placeholder="结束时间(修改时间)">

</div>

</div>

js代码

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

layui.use([ 'laydate'], function(){

var $ = layui.$;

var laydate = layui.laydate;

var nowTime = new Date().valueOf();

var max = null;

var start = laydate.render({

elem: '#start_time',

type: 'datetime',

max: nowTime,

btns: ['clear', 'confirm'],

done: function(value, date){

endMax = end.config.max;

end.config.min = date;

end.config.min.month = date.month -1;

}

});

var end = laydate.render({

elem: '#end_time',

type: 'datetime',

max: nowTime,

done: function(value, date){

if($.trim(value) == ''){

var curDate = new Date();

date = {'date': curDate.getDate(), 'month': curDate.getMonth()+1, 'year': curDate.getFullYear()};

}

start.config.max = date;

start.config.max.month = date.month -1;

}

});

根据开始时间 动态限制结束时间 知识点

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

type: 'datetime', 是带时分秒的 date 是不带时分秒的

 

layui.use('laydate', function(){

   /* lay('.layui-input').each(function(){

  laydate.render({

    elem: this

    ,trigger: 'click'

    ,change: function(value, date, endDate){

    console.log(value); //得到日期生成的值,如:2017-08-18

    console.log(date); //得到日期时间对象:{year: 2017, month: 8, date: 18, hours: 0, minutes: 0, seconds: 0}

    console.log(endDate); //得结束的日期时间对象,开启范围选择(range: true)才会返回。对象成员同上。

    }

  });

});  */

var $ = layui.$;

    var laydate = layui.laydate;

   var nowTime = new Date().valueOf();

    var max = null;

       var start = laydate.render({

        elem: '#start_time',

        type: 'datetime',

        btns: ['clear', 'confirm'],

        done: function(value, date){

            endMax = end.config.max;

            end.config.min = date;

            end.config.min.month = date.month -1;

        },

        change: function(value, date, endDate){

        var timestamp2 = Date.parse(new Date(value));

timestamp2 = timestamp2 / 1000;

            end.config.min = timestamp2;

            end.config.min.month = date.month -1;

    }

    });

    var end = laydate.render({

        elem: '#end_time',

        type: 'date',

        done: function(value, date){

        console.log(" ======  "+date);

            if($.trim(value) == ''){

                var curDate = new Date();

                date = {'date': curDate.getDate(), 'month': curDate.getMonth()+1, 'year': curDate.getFullYear()};

            }

            start.config.max = date;

            start.config.max.month = date.month -1;

        }

    });

});

通过以上代码,就已经可以实现动态改变开始时间最大值与结束时间最小值的改变了。下面来说一下容易遇到的坑:

坑一 :laydate.render无法重复渲染,当laydate.render对应一个elem已经渲染过一次之后,我们是无法通过再次渲染来修改其中的max值与min值的。

坑二 :startDate.config.max与endDate.config.min是一个对象,不是一个字符串,在网上看到一个人不负责任的给了这么一句话,endDate.config.min="2017-01-01";说可以设置,我居然信了他的邪掉进坑里半天。实际这里得到的是一个对象,不同于在我们渲染时的min与max了,直接将字符串赋值必然没有效果。

坑三:dates的格式虽然与endDate.config.min格式相同但是直接让endDate.config.min=dates你会发现并不是你想要的结果,是因为虽然dates中的数据是你选择的日期,可是endDate.config.min中设置的month的值却比你输入的month的值大了一个月,因此假如你选的开始日期是11月13日,直接赋值给了endDate.config.min之后你会发现结束日期的最小日期变成了12月13日,因此我们需要将dates中的月份值减一后再赋值给endDate.config.min。

更多layui知识请关注layui使用教程栏目。

以上就是layui时间控件清空之后无法正常使用的问题的详细内容,更多文章请关注木庄网络博客

返回前面的内容

相关阅读 >>

在vue中怎么使用layui

layui中layer的宽高怎么自定义设置?

如何解决vue中layui报错问题

layui后台框架的搭建详解

layui框架的flow组件常见用法总结

layui的iframe跳转链接与页面按钮跳转相关介绍

layui是开源的吗

layui下拉选中的值怎么回填

layui和easyui有什么区别

layui框架分页设置详解

更多相关阅读请进入《layui》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...