【发布时间】:2017-11-22 11:44:27
【问题描述】:
我正在构建一个日历选择器来伴随(并替换某些浏览器提供的默认日历日期选择器)input 或 type="date"。
据记载,为了“简单”,Chrome(可能还有其他浏览器)的日期输入允许输入无效日期,但它们将无法验证(即 31/06/2017 可以输入,但会产生一个“值“的”)。
我的问题是:当输入的日期无效时,如何获取无效日期的值?
var date_input = document.getElementById('date_input');
var output_div = document.getElementById('output_div');
output_div.innerHTML = date_input.value;
date_input.addEventListener('input', function(e){
output_div.innerHTML = e.target.value;
});
<p>To quickly input an invalid date, select the day field and press the down arrow (on Chrome OSX this displays an empty div below the input instead of the invalid date). This will select 31/06/2017</p>
<input type="date" value="2017-06-01" id="date_input" />
<br />
<br />
<div id="output_div" />
【问题讨论】:
-
如果使用的是输入类型日期,那么日期值怎么会无效呢?
-
@sajalsuraj,如前所述,您可以输入(手动或通过按箭头键)日期,例如
31/06/2017(只有 30 天)六月)。输入允许这个值被输入和显示,但在使用Javascript查询时不返回它作为它的“值”)
标签: javascript html validation html5-input-date