简体   繁体   English

如何在选择将来的日期时禁用HTML日期输入类型?

[英]How to disable html date input type in choosing future dates?

I have a code and just last year, it was still working. 我有一个代码,就在去年,它还在工作。 What it does is that it disables user to pick future dates. 它的作用是使用户无法选择将来的日期。 Now that it's January, it doesn't seem to be working. 现在是一月,它似乎不起作用。 I do not understand why. 我不理解为什么。 Please help me fix this. 请帮我解决这个问题。 Your help will be appreciated. 您的帮助将不胜感激。

This is the input: 这是输入:

<input id="reqDate" class="form-control col-md-7 col-xs-12" name="reqDate" required="required" type="date">

Javascript: 使用Javascript:

<script type="text/javascript">
          var input = document.getElementById("reqDate");
          var today = new Date();
          var day = today.getDate();

          // Set month to string to add leading 0
          var mon = new String(today.getMonth()+1); //January is 0!
          var yr = today.getFullYear();

            if(mon.length < 2) { mon = "0" + mon; }

            var date = new String( yr + '-' + mon + '-' + day );

          input.disabled = false; 
          input.setAttribute('max', date);
</script>

By the way, I am using this code in a .php file. 顺便说一句,我在.php文件中使用此代码。

Like you have used, the native input[type="date"] element supports a maximum date value: 就像您使用过的一样,本机input[type="date"]元素支持最大日期值:

Enter a date before 1980-01-01:
<input type="date" name="bday" max="1979-12-31">
http://www.w3schools.com/tags/att_input_max.asp

The JavaScript you are using to set the date does not zero-pad the day, so for the first of January, you would get the date max="2016-01-1". 您用于设置日期的JavaScript不会对日期进行零填充,因此对于1月1日,您将获得日期max =“ 2016-01-1”。 To keep with your JavaScript version, sero pad the date with: 要保持您的JavaScript版本,请使用以下命令来填充日期:

<script>
      var input = document.getElementById("reqDate");
      var today = new Date();
      var day = today.getDate();

      // Set month to string to add leading 0
      var mon = new String(today.getMonth()+1); //January is 0!
      var yr = today.getFullYear();

        if(mon.length < 2) { mon = "0" + mon; }
        if(day.length < 2) { dayn = "0" + day; }

        var date = new String( yr + '-' + mon + '-' + day );

      input.disabled = false; 
      input.setAttribute('max', date);
</script>

With this being a PHP script, to simplify your code you could use: 以此为PHP脚本,可以简化代码,可以使用:

<input type="date" name="bday" max="<?=date('Y-m-d')?>">

NB Support for input[type="date"] varies between browsers (see http://caniuse.com/#feat=input-datetime ). 注意:不同浏览器对input[type="date"]有所不同(请参阅http://caniuse.com/#feat=input-datetime )。

You can confirm the max value used is in the correct supported RCF3339 format by inspecting the element in the browser inspector. 您可以通过检查浏览器检查器中的元素来确认使用的最大值是正确的受支持的RCF3339格式。

Did you try using min attribute in the input. 您是否尝试在输入中使用min属性。 In html5 we can specify min or max attributres in proper date format YYYY-MM-DD 在html5中,我们可以采用正确的日期格式YYYY-MM-DD指定最小或最大属性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM