admin管理员组文章数量:1313962
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();
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.
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.
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.
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();
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.
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.
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.
Share Improve this question edited May 10, 2016 at 2:38 Matthew Herbst asked Oct 28, 2014 at 18:04 Matthew HerbstMatthew Herbst 32k26 gold badges90 silver badges136 bronze badges 2- its not possible to get AM/PM it's a ISO date format (meaning 24 hour) so you would have to convert it... – Endless Commented Oct 28, 2014 at 18:15
- jQuery .val() already does the conversion for me. The problem I have is when the datetime is not wholly qualified. – Matthew Herbst Commented Oct 28, 2014 at 18:18
1 Answer
Reset to default 6The 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.
本文标签: javascriptHot to get incomplete datetimelocal input valuesStack Overflow
版权声明:本文标题:javascript - Hot to get incomplete datetime-local input values - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741928160a2405417.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论