HTML5 Input Type Date — Default Value to Today?

2019-01-01 12:24发布

The new HTML5 input types are great. Opera's new built-in date picker is a breeze, and Chrome has at least supported the new input type with a spin-wheel implementation.

But is there any way to set the default value of the date field to today's date? With Opera, I can choose 'Today' from the date picker, and as soon as I click on either of the step buttons in Chrome, it increments/decrements from today's date.

I'm not shy to code a solution to this minor problem, but it seems silly to me that both of the browsers are fully aware of the current date but won't automatically just pop it in (at least as a placeholder).

标签: html5 date input
25条回答
几人难应
2楼-- · 2019-01-01 12:53

A simple solution !!

<input class="set-today" type="date">
<script type="text/javascript">
window.onload= function(){
document.querySelector('.set-today').value=(new Date()).toISOString().substr(0,10));
}
</script>
查看更多
登录 后发表回答