今天有用到时间选择插件 记录以下
laydate官方网站: https://www.layui.com/laydate/
官方下载该js压缩包,放在所对应的目录下,直接引入laydate.js 文件(莫要动原有文件)
<script src="./layDate/laydate/laydate.js"></script> <!-- 改成你的路径 -->
给所在的input框写入id
<input type="text" id="test1">
js中写入,并运行
<script>
//执行一个laydate实例
laydate.render({
elem: '#test1'
,type: 'month', // "date" 表示年-月-日 "month" 表示年-月 "year" 表示年
});
</script>
简单效果如下:
选定日期后可监听所选日期的变化,方法为change 如下:
<script>
//执行一个laydate实例
laydate.render({
elem: '#test1',
type: 'month', // "date" 表示年-月-日 "month" 表示年-月 "year" 表示年
change:function(value,date) { // change 每点一次都会监听一遍
console.log(value,"这是value"); //value 为所选的日期 跟type参数相关 ,
console.log(date,"这是date"); // date 一个对象包裹的时间,年月日键值对
}
});
</script>
选定日期后的回调,方法为done 如下:
<script>
laydate.render({
elem: '#test1',
type: 'month',
done:function(value,date) { // 选定日期点击清空、现在、确定均会触发 不同change
console.log(value,"这是value");
console.log(date,"这是date");
}
});
</script>
今天遇到了一个小问题,页面加载完之后,方法只触发一次,每次选择毫无反应,后来给input一个点击事件然后进行触发才能实现,不知是什么问题,如果有大神遇到,还望留言解惑。(也有可能是我写的哪里冲突了),好了,言归正传:
var reportLogTime = function(node,type) {
var today = new Date();
laydate.render({
elem: node,
type:type,
value:newTime,
done:function(value, date, endDate){
}
});
};
$('#report_log_time_day').click(function(){
reportLogTime('#report_log_time_day',"day");
});
$('#report_log_time_month').click(function(){
reportLogTime('#report_log_time_month','month');
});
$('#report_log_time_year').click(function(){
reportLogTime('#report_log_time_year','year');
});
请忽略我的代码实现,重点是给套了一层click事件才能触发,有点懵。
只是敲门砖,详细细节请参考官方网站,请看抬头。