如何在输入元素中设置默认月份? [英] How to set a default month in an input element?

查看:62
本文介绍了如何在输入元素中设置默认月份?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一个像这样的输入元素:

Say I have an input element like this:

<input type="month">

如何将此输入元素的默认值设置为当前月?

How can I set the default value of this input element to the current month?

推荐答案

您可以使用一些javascript:

You may use some javascript:

const monthControl = document.querySelector('input[type="month"]');
const date= new Date()
const month=("0" + (date.getMonth() + 1)).slice(-2)
const year=date.getFullYear()
monthControl.value = `${year}-${month}`;

<input type="month">

这篇关于如何在输入元素中设置默认月份?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆