日历--给input赋值

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();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值