简体   繁体   English

HTML:输入类型日期和月份范围

[英]HTML: Input type date and range of months

I'm trying to make an HTML form with input type="date" which will be a range of months but not years.我正在尝试使用input type="date"制作一个 HTML 表单,该表单将是几个月而不是几年的范围。 For example I need the user to select a date with a range from March to October, but with all available years (2019, 2020, 2021...).例如,我需要用户 select 日期范围从 3 月到 10 月,但包含所有可用年份(2019、2020、2021 ......)。

I know about option of attributes min and max but we must fill the entire specific date and I only need a range of months.我知道属性minmax的选项,但我们必须填写整个特定日期,我只需要几个月的范围。

CODE of min and max : minmax代码:

<input type="date" min="2019-03-01" max="2019-10-31">

Is there any option to do this with clear HTML or must we use JavaScript?是否有任何选项可以使用清除 HTML 或我们必须使用 JavaScript?

Unfortunately, there is no way to eliminate the 'year' portion of the HTML date input, which makes some sense given that it has such wildly different implementations on different platforms.不幸的是,没有办法消除 HTML 日期输入的“年份”部分,考虑到它在不同平台上的实现如此不同,这是有道理的。

JS will let you build what you want, but it will be more difficult to ensure cross-platform reliability. JS 会让你构建你想要的东西,但是要确保跨平台的可靠性会更加困难。 Here's one option, you can use a date input but restrict it and then strip the response with JS, though that does not actually hide the option to change the year.这是一个选项,您可以使用日期输入但对其进行限制,然后使用 JS 去除响应,尽管这实际上并没有隐藏更改年份的选项。

How to hide the year part in Html <input type="date"> calendar panel? 如何在 Html <input type="date"> 日历面板中隐藏年份部分?

Here's a similar SO answer that could lead you down a path to manipulate jquery's datepicker to do what you want:这是一个类似的 SO 答案,它可能会引导您走上一条操作 jquery 的 datepicker 来做您想做的事情的路径:

https://stackoverflow.com/a/10243049/1650488 https://stackoverflow.com/a/10243049/1650488

Ultimately, there are a lot of benefits with sticking to the native HTML elements instead of rolling your own solution, so personally I'd look at the workflow and try to make it make sense with the native datepicker.最终,坚持使用本机 HTML 元素而不是滚动您自己的解决方案有很多好处,因此我个人会查看工作流程并尝试使用本机日期选择器使其有意义。

If the native element really doesn't work, It might make sense to build your own input such as selecting the date and month range separately, but then you could run in to troubles with differing lengths of months and it will be hard to manage accessibility devices such as screen readers.如果本机元素真的不起作用,构建自己的输入(例如分别选择日期和月份范围)可能是有意义的,但是您可能会遇到不同月份长度的麻烦,并且很难管理可访问性屏幕阅读器等设备。

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

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