简体   繁体   English

如何显示日历,但不显示输入类型=日期的输入框?

[英]How can I show calendar but not input box of input type=date?

I have a default value set in my <input type=date> and I would like to display only the calendar and not the input box. 我在<input type=date>设置了默认值,并且我只想显示日历而不是输入框。

I am trying to do it with Javascript but without success as I show below: 我正在尝试使用Javascript执行此操作,但未成功,如下所示:

 var date = document.getElementById('date'); date.click(); 
 input[type="date"]{ display: none; } 
 <input id="date" type="date" value="2016-06-02" readonly> 

As you can see, nothing is displayed (it does not seem that click event could be apply to that input because it does not matter if I have it displayed or not, click event does not work). 如您所见,没有显示任何内容(似乎click事件不能应用于该输入,因为无论是否显示都无所谓,单击事件不起作用)。

I would like to show only the calendar(not the input) and make it read-only (already done) on the screen all the time that the webpage will be displayed. 我想只显示日历(而不是输入),并使其在显示网页的所有时间中都保持只读(已完成)。

Is it possible? 可能吗?

EDIT: For a better clarification, when I refer to calendar I mean to the calendar that is shown when you click on the input type=date . 编辑:为了更好的说明,当我提到日历时,我指的是单击input type=date时显示的日历。

Thanks in advance! 提前致谢!

I think that you can use JQuery UI which is a plugin to create user-friendly UI elements. 我认为您可以使用JQuery UI这个插件来创建用户友好的UI元素。

Check this link . 检查此链接

In the example you can see the code that it is used. 在示例中,您可以看到使用的代码。 It is very easy, you only need to invoke the datepicker, and it is done by these lines: 这很容易,您只需要调用datepicker,就可以通过以下几行完成:

$(function() {
    $( "#datepicker" ).datepicker();
});

So you want to display onload the native calendar, here is a solution : 因此,您想显示本机日历的onload,这是一个解决方案:

1 - As you can't hide the input AND show the calendar, set its opacity to 0 : 1-由于您无法隐藏输入并显示日历,因此将其不透明度设置为0:

input[type="date"]{
  opacity:0;
}

2 - As Michał Šrajer showed in this post you can trigger the event via firing F4 key event : 2-正如MichałŠrajer在这篇文章中所示,您可以通过触发F4键事件来触发事件:

function openPicker(inputDateElem) {
    var ev = document.createEvent('KeyboardEvent');
    ev.initKeyboardEvent('keydown', true, true, document.defaultView, 'F4', 0);
    inputDateElem.dispatchEvent(ev);
}

var cal = document.querySelector('#cal');
cal.focus();
openPicker(cal);

This will hide the input and show the calendar. 这将隐藏输入并显示日历。

Remember that this will not work in Firefox 请记住,这在Firefox中不起作用

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

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