Setting the value of HTML5 input type=week thru jquery

心不动则不痛 提交于 2019-12-12 01:49:26

问题


How to set the default value of HTML5's input type="week" in Javascript/Jquery? I'm using moment.js to make things easier.

HTML

<input type="week" id="weekpicker" />

Javascript

$('#weekpicker').val(moment().format('YYYY-MM-DD'));

The code above doesn't work.


回答1:


Here's how I did it (using momentjs): http://momentjs.com/docs/#/get-set/

$('#weekpicker').val(moment().weekYear() + "-W" + moment().week());

First I get the weekYear (not the year, Because the first day of the first week does not always fall on the first day of the year, sometimes the week-year will differ from the month year. source: http://momentjs.com/docs/#/get-set/week-year/) then I concatenated a letter "W" as the format requires, then I get the week. I added it all together and got: 2014-W47




回答2:


Using the default format it will be like this: 'yyyy-Www'

<input type="week" value="2014-W02">

Above gives the 2nd week of 2014.

Example: http://jsfiddle.net/oLs6Lseh/1/



来源:https://stackoverflow.com/questions/26966574/setting-the-value-of-html5-input-type-week-thru-jquery

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