首页 > 学院 > 开发设计 > 正文

日历--给input赋值

2019-11-07 23:35:36
字体:
来源:转载
供稿:网友

HTML5 新加了一些标签,最近用到了 inputtype='date' ,可用来制作简单的日历。(PS:在 HtmljavaScript 中单引号和双引号貌似没什么区别,在写代码时自己注意一下即可。) 我想做成如下效果:

日历效果图
就是在刚进入界面时,就能显示当前的日期,所以需要给 input 赋值。从网上找到了两种赋值方法:

一、value赋值法

$("#start_date")[0].value = nowDate();

这个赋值一直出错的原因是 $("#start_date")[0].value = nowDate(); 没有加[0],这个是由于本人对 JQuery 理解不深

二、val()赋值法

$("#start_date").val(nowDate());

获取当前时间 nowDate() 方法

// 获取当前时间function nowDate(){ now = new Date(); // 当前日期 if((now.getMonth() + 1) < 10){ if(now.getDate()<10){ // 当前日期 today = now.getFullYear() + '-' +'0' + (now.getMonth() + 1) + '-' +'0' + now.getDate(); }else{ // 当前日期 today = now.getFullYear() + '-' +'0' + (now.getMonth() + 1) + '-' + now.getDate(); } }else{ if(now.getDate()<10){ // 当前日期 today = now.getFullYear() + '-' + (now.getMonth() + 1) + '-' +'0' + now.getDate(); }else{ // 当前日期 today = now.getFullYear() + '-' + (now.getMonth() + 1) + '-' + now.getDate(); } } console.log("today is day--->" + today); return today;}

<input type = "date" id="start_time"/> 的效果图

设置日期

获取input中的值

var start_time = $("#start_date").val();var end_time = $("#end_date").val();
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表