问题
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