doukan1258
2017-03-16 06:42
浏览 53

禁用输入类型“日期”中的返回日期条目

I am using input type date in my project instead of datepicker. I want to restrict back dates from current date in input type date. I know about how to restrict back dates in datepicker but i dont want to use it.

Hence Please suggest is it possible to restrict back dates in input type date (html).

图片转代码服务由CSDN问答提供 功能建议

我在项目中使用输入类型日期而不是datepicker。 我想在输入类型日期限制当前日期的日期。 我知道如何限制datepicker中的日期,但我不想使用它。

因此请建议是否可以在输入类型日期(html)中限制日期。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dtr84664 2017-03-16 06:45
    已采纳

    Change the min value of input in Javascript, like this:

        document.getElementById("myDate").min = "2017-03-16";
    
    点赞 打赏 评论
  • dsfsdf7852 2017-03-16 06:57

    If you want to have dynamic disabled past dates,

    Try this,

    today =  new Date(); // instantiating current date
    day = (today.getDate().length ==1 ? "0"+today.getDate() : today.getDate());
    month = parseInt(today.getMonth())+1;
    month = (today.getMonth().length == 1 ? "0"+month : month); 
    year = today.getFullYear();
    current_date = new String(year+'-'+month+'-'+day); // creating new date and as per requirement of format of min for date input type
    document.getElementById("myDate").setAttribute('min',current_date); // assigning new date to element
    

    I hope this will help.

    点赞 打赏 评论
  • dousi1097 2017-03-16 07:17

    Try this

    var inputMyDate = document.querySelector('input#finaldate');
    
    inputMyDate.addEventListener('input', function() {  
        var current = this.value;               
        var today = new Date();     
        var dd = today.getDate();       
        var mm = today.getMonth()+1;
        var yyyy = today.getFullYear();
        if(dd<10){
            dd='0'+dd;
        } 
        if(mm<10){
            mm='0'+mm;
        } 
        var today = yyyy+'-'+mm+'-'+dd;                     
        if (current < today){
            document.getElementById('finaldate').value = today;
        }       
    });
    
    点赞 打赏 评论

相关推荐 更多相似问题