在 HTML 5 输入类型日期上禁用周末
问题描述:
是否可以为 HTML 5 输入类型的日期禁用所有周末?
Is it possible to disable all weekends for HTML 5 input type date?
<input id="date1" size="60" type="date" format="MM/DD/YYYY" placeholder="MM/DD/YYYY" />
答
只需检查当天.如果是周末,您可以重置该值并告诉用户选择其他内容.
Just check the day. If it's a weekend you can reset the value and tell the user to pick something else.
const picker = document.getElementById('date1');
picker.addEventListener('input', function(e){
var day = new Date(this.value).getUTCDay();
if([6,0].includes(day)){
e.preventDefault();
this.value = '';
alert('Weekends not allowed');
}
});
<input id="date1" size="60" type="date" format="MM/DD/YYYY" placeholder="MM/DD/YYYY" />