JavaScript禁止日期格式警告



每当我尝试使用JavaScript重置日期输入时,就像这样:

//input_id is an id of a date input
document.getElementById(input_id).value = "0000-00-00";

我在控制台中得到一个警告:

The specified value "0000-00-00" does not conform to the required format, "yyyy-MM-dd".

有没有人知道一种方法来抑制这个警告,使它不会显示?JS继续平稳运行,但我想摆脱这些警告。

如果你有另一种方法重置日期输入(不引发警告),我会很高兴听到。

您得到警告是因为年、月和日从1开始,而不是0。0是一个无效值。但是有一种更简单的方法来重置输入元素。

您可以将值设置为空字符串,这是初始默认值。

var di = document.createElement("input");
di.type = "date";
console.log(di.value); // outputs ""
document.body.appendChild(di);
// change the value if you want
// to reset:
di.value = "";

可以指定一个空字符串值

document.getElementById(input_id).value = "";

在Javascript调用的底部

console.clear();

清除控制台中所有的警告和错误。

您也可以用自己的代码替换console.warn,如:

console.warn = function () { };

相关内容

最新更新