简体   繁体   English

如何在更改时关闭日期选择器 <input type=“date” /> HTML5输入日期元素

[英]How to close the date picker on change for <input type=“date” /> HTML5 Input date element

I have a HTML5 <input type="date" /> field, and I want to instantly respond to changed dates. 我有一个HTML5 <input type="date" />字段,我想立即回复更改日期。

Currently, the input field partially obscures the data that changes when the date is changed, so I'd prefer to auto-close the calendar picker after a date is changed. 目前,输入字段部分遮盖了日期更改时更改的数据,因此我希望在更改日期后自动关闭日历选择器。

I've tried to .blur() on change, but this doesn't have any effect. 我试过改变.blur(),但这没有任何效果。 The event fires, and I can get the new date value, but the date picker is not hidden. 触发事件,我可以获取新的日期值,但不会隐藏日期选择器。 Any suggestions are welcome. 欢迎任何建议。

  .bind('change', function(){
    var input = $(this);
    if(input.is(':valid'))
    {
      input.blur();
    }
  });

[UPDATE] [UPDATE]

By now, I have also resorted to adding an additional input element on the page ( <input type="text" id="hideMyCalendar" /> ) and instead of input.blur(); 到目前为止,我还在页面上添加了一个额外的输入元素( <input type="text" id="hideMyCalendar" /> )而不是input.blur(); to do a $('#hideMyCalendar').focus(); 做一个$('#hideMyCalendar').focus(); but this also doesn't hide the picker, even though the focus is visually brought to a different control. 但这也不会隐藏选择器,即使焦点在视觉上被带到不同的控件。 By now, I assume, the only possible way would be to call a chrome (webkit) specific method, but I haven't fount such a method to exist (yet). 到目前为止,我认为,唯一可行的方法是调用chrome(webkit)特定方法,但我还没有将这样的方法存在(尚未)。

01/30/14 This no longer works 01/30/14这不再有效

This is ugly, but it works for Chrome. 这很难看,但适用于Chrome。

function closeDate() {
    $('#txtDate').attr('type', 'text');
    $('#txtDate').attr('type', 'date');
}

This is the only way I was able to close the html5 date after selecting a date. 这是我在选择日期后能够关闭html5日期的唯一方法。 Just remove the date attribute, then apply it again. 只需删除日期属性,然后再次应用它。 The value is retained, and it just looks as if it was closed manually. 该值保留,看起来好像是手动关闭的。

UPDATE: 更新:

This is the method I use to make all date types close like this: 这是我用来使所有日期类型关闭的方法,如下所示:

$(document).ready(function () {
    //make all date html5 close on change in chrome
    $('input[type="date"]').change(function () {
        closeDate(this);
    });

});

function closeDate(dateInput) {
    $(dateInput).attr('type', 'text');
    $(dateInput).attr('type', 'date');
}

so I'd prefer to auto-close the calendar picker after a date is changed 所以我希望在更改日期后自动关闭日历选择器

HTML 5 input type="date" does not specify how this input should be realized by browsers. HTML 5 input type="date"未指定浏览器应如何实现此输入。 It can be a graphical datepicker, it can be a simple validation on a user input - whatever the browser vendor wants to implements. 它可以是图形日期选择器,它可以是对用户输入的简单验证 - 无论浏览器供应商想要实现什么。

So, hiding the "datepicker", which is in fact browser dependend, is not possible in a cross-browser way. 因此,隐藏“datepicker”(实际上是浏览器依赖)是不可能以跨浏览器的方式进行的。

However, you can use the jQuery UI datepicker , where you have full control how it is shown and hidden. 但是,您可以使用jQuery UI datepicker ,您可以完全控制它的显示和隐藏方式。

You said (in your comment ): 你说(在你的评论中 ):

I prefer the HTML5 input element because I hope this brings unity to the web. 我更喜欢HTML5输入元素,因为我希望这能为网络带来统一性。

And yet you are willing to call a webkit specific method? 然而,您是否愿意调用特定于webkit的方法? (in the update to the question ): (在问题的更新中 ):

By now, I assume, the only possible way would be to call a chrome (webkit) specific method, but I haven't fount such a method to exist (yet). 到目前为止,我认为,唯一可行的方法是调用chrome(webkit)特定方法,但我还没有将这样的方法存在(尚未)。

I think that defies the purpose. 我认为这违背了目的。 Then you will need an Opera specific method, an IE specific method, a Firefox specific method and so on... no more unity to the web . 然后你需要一个特定于Opera的方法,一个IE特定的方法,一个特定于Firefox的方法等等......不再需要统一网络

If you really want a single user experience across browsers, then go for jQuery UI . 如果您真的想要跨浏览器的单一用户体验,那么请转到jQuery UI It doesn't only give you the versatility to do what you want (as Nil'z points out ), it also give you the same behaviour across heterogeneous browsers. 它不仅为您提供了所需的多功能性(如Nil'z指出的那样 ),它还可以在异构浏览器中提供相同的行为。


Instead of using HTML5 where available. 而不是在可用的地方使用HTML5。 What you should want to do is to fall back to HTML5 when JavaScript is not available. 您应该做的是在JavaScript不可用时回退到HTML5。 That is using unobstructive JavaScript principles . 这是使用无阻碍的JavaScript原则 In fact jQuery is designed to do that by default... 实际上jQuery是默认设计的...

Just create your HTML5 (and HTML4 compatible) field: 只需创建HTML5(和HTML4兼容)字段:

<input type="date" name="date" id="date" value="" />

Your jQuery: 你的jQuery:

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.0/jquery.min.js></script>  
<script type="text/javascript" src="js/jquery-ui.js"></script>

And your script: 你的脚本:

<script type="text/javascript">
    $('#date').datepicker();   
</script>

The end result will be that all browsers where jQuery can run will use the jQuery based datepicker, if JavaScript is not available then it will use HTML5 datepicker, and finally if the browser doesn't support HTML5 then it will be just a text field. 最终的结果是jQuery可以运行的所有浏览器都将使用基于jQuery的datepicker,如果JavaScript不可用,那么它将使用HTML5 datepicker,最后如果浏览器不支持HTML5,那么它将只是一个文本字段。 That's graceful degradation. 这是优雅的退化。


And of course you will use the hide method from jQuery datepicker : 当然,您将使用jQuery datepicker中hide方法

$( ".selector" ).datepicker( "hide" );

Note: +1 to Nil'z 注意:+1到Nil'z


I know I didn't answer the question, but this solves the problem. 我知道我没有回答这个问题,但这解决了这个问题。

Try this: 试试这个:

$( ".selector" ).datepicker( "hide" );

Reference: http://api.jqueryui.com/datepicker/#method-hide 参考: http//api.jqueryui.com/datepicker/#method-hide

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

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