如何修复gijgo DatePicker的startDate功能失效问题(不更换CDN)
解决gijgo@1.9.13 DatePicker startDate: '+2d' 不生效的问题
gijgo@1.9.13版本的DatePicker组件不支持直接用'+2d'这种相对字符串格式设置startDate,该版本要求startDate参数必须传入Date对象。要在不更换CDN的前提下实现需求,只需手动计算出目标日期的Date对象即可。
修改后的完整代码如下:
<html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Bootstrap 4 DatePicker</title> <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous"> <script src="https://unpkg.com/gijgo@1.9.13/js/gijgo.min.js" type="text/javascript"></script> <link href="https://unpkg.com/gijgo@1.9.13/css/gijgo.min.css" rel="stylesheet" type="text/css" /> </head> <body> <input id="datepicker" width="276" /> <script> // 计算当前日期后推2天的Date对象 const currentDate = new Date(); currentDate.setDate(currentDate.getDate() + 2); $('#datepicker').datepicker({ startDate: currentDate }); </script> </body> </html>
代码说明
- 先创建当前日期的
Date实例:new Date() - 使用
setDate()方法将日期向后偏移2天:currentDate.getDate() + 2会获取当前日期的天数并加2,再通过setDate()设置回日期对象 - 将计算好的Date对象赋值给
startDate参数,此时日期选择器会自动禁用两天前的所有日期
内容的提问来源于stack exchange,提问作者Amol_G
相关产品推荐
相关产品推荐

