从 html5 datepicker 禁用某些日期 [英] Disable certain dates from html5 datepicker

查看:24
本文介绍了从 html5 datepicker 禁用某些日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用时是否可以禁用日期我想禁用一个场景的当前日期和其他场景的未来日期.我应该如何禁用日期?

Is it possible to disable dates when I use I want to disable current date for one scenario and future dates for other scenario. How should I disable the dates?

推荐答案

您可以将 minmax 属性添加到 input type=date.日期必须采用 ISO 格式 (yyyy-mm-dd).尽管用户可以在不使用日期选择器的情况下手动输入无效日期,但许多移动浏览器和当前版本的 Chrome 都支持此功能.

You can add a min or max attribute to the input type=date. The date must be in ISO format (yyyy-mm-dd). This is supported in many mobile browsers and current versions of Chrome, although users can manually enter an invalid date without using the datepicker.

<input name="somedate" type="date" min="2013-12-25">

minmax 属性必须是一个完整的日期;没有办法指定今天"或+0".为此,您需要使用 JavaScript 或服务器端语言:

The min and max attributes must be a full date; there's no way to specify "today" or "+0". To do that, you'll need to use JavaScript or a server-side language:

var today = new Date().toISOString().split('T')[0];
document.getElementsByName("somedate")[0].setAttribute('min', today);

http://jsfiddle.net/mblase75/kz7d2/

只排除今天,同时允许过去或未来的日期,在这里不是一个选项.但是,如果您的意思是希望明天是 min 日期(将今天和所有过去的日期清空),请参阅 这个问题today增加一天.

Ruling out only today, while allowing past or future dates, is not an option with here. However, if you meant you want tomorrow to be the min date (blanking out today and all past dates), see this question to increment today by one day.

与涉及 HTML 表单的所有其他情况一样,无论您如何在客户端对其进行约束,您都应该始终在服务器端验证该字段.

As in all other cases involving HTML forms, you should always validate the field server-side regardless of how you constrain it client-side.

这篇关于从 html5 datepicker 禁用某些日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆