假设我有这样一个输入元素:
<input type="month">
如何将此输入元素的默认值设置为当前月份?
发布于 2018-11-07 03:18:27
您可以使用一些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">
发布于 2018-11-07 03:20:13
要设置月份输入(input[type="month"]
)的值,只使用值(yyyy-MM
)中的年份和月份,例如:
<input type="month" id="txtMonth" value="2018-11" />
将显示月份为Novermber (在支持月份输入类型的浏览器中,支持是不完整的)。
要使用javascript填充字段,可以执行以下操作:
var txtMonth = document.getElementById('txtMonth');
var date = new Date();
var month = "0" + (date.getMonth() + 1);
txtMonth.value = (date.getFullYear() + "-" + (month.slice(-2)));
<input type="month" id="txtMonth" value="2018-11" />
发布于 2018-11-07 03:20:51
您必须构造新日期并查询您的输入,然后执行以下操作:
let date = new Date();
let month = `${date.getMonth() + 1}`.padStart(0, 2);
let year = date.getFullYear();
document.getElementById("month").value = `${year}-${month}`
<input id="month" type="month" value="2012-3-23">
https://stackoverflow.com/questions/53188184
复制相似问题