热得到不完整的datetime-local输入值 [英] Hot to get incomplete datetime-local input values

查看:168
本文介绍了热得到不完整的datetime-local输入值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的页面上有以下表单:

I have the following form on my page:

<input id='startDate' type='datetime-local' step=1 name='startDate'>

我使用此代码获取字段的值:

I use this code to get the value of the field:

var start = $('#startDate').val();

我的问题是输入字段的值保持未定义,直到其每个部分都被填写,包括小时,分钟,秒和AM / PM。我希望能够从表单中获得一个值,只要选择一年。

My problem is that the value of the input field remains undefined until every part of it has been filled out, including hours, minutes, seconds, and AM/PM. I would like to be able to get a value from the form with as little as just the year selected.

我该怎么做?我很乐意清除未填写的值,但我无法弄清楚如何获取用户填写的datetime-local的部分。

How can I go about doing this? I'm happy to zero out the values not filled out, but I can't figure out how to get the part of the datetime-local that the user did fill out.

我知道我可以使用 timepicker addon 进行jQuery UI的datepicker,但是我不真的很想去这条路线 - 我喜欢Chrome的实现明显好些。

I know I can do this using the timepicker addon for jQuery UI's datepicker, but I don't really want to go this route - I like the Chrome implementation significantly better.

推荐答案

W3C日期状态规格定义了消毒算法:



  • 值消毒算法如下:如果元素的值不是有效日期字符串,然后将其设置为空字符串。

每当设置值输入元素,如 DOM输入值规格

Which is invoked whenever setting the value of the input element, as defined in DOM input value spec:


  • 在获取时,它必须返回元素的当前值。在设置时,必须将元素的值设置为新值,将
    元素的脏值标志设置为true,如果元素的类型属性的当前值为,则调用值消毒
    算法
    状态定义
    一个,然后,如果元素具有文本条目光标位置,则
    将文本条目光标位置移动到文本字段的末尾,
    取消选择任何所选文本并重置选择方向
    无。

  • On getting, it must return the current value of the element. On setting, it must set the element's value to the new value, set the element's dirty value flag to true, invoke the value sanitization algorithm, if the element's type attribute's current state defines one, and then, if the element has a text entry cursor position, should move the text entry cursor position to the end of the text field, unselecting any selected text and resetting the selection direction to none.

所以值当日期无效时,属性将始终为空字符串。没有任何属性指定为原始/脏/用户键入的文本值,因为它不是文本输入。

So the value property will always be an empty string when the date is not valid. There doesn't seem to be any property for "original/dirty/user-typed text value" specified as it is not a text input after all.

IMO是一个好东西,它有助于表单验证,因为无效日期被视为伪造值(空字符串),它也使浏览器更自由地实现日期输入的UI。

IMO it is a good thing, it facilitates form validation as invalid dates are treated as falsy values (empty strings), it also leaves browsers to more freely implement the UI for date inputs.

这篇关于热得到不完整的datetime-local输入值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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