Hot to get incomplete datetime-local input values

混江龙づ霸主 提交于 2019-11-28 11:21:14

The W3C Date State spec defines the sanitization algorithm:

  • The value sanitization algorithm is as follows: If the value of the element is not a valid date string, then set it to the empty string instead.

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

  • 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 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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!