html5 输入类型日期输入事件错误地以空值触发 [英] html5 input type date input event incorrectly fires with empty value

查看:25
本文介绍了html5 输入类型日期输入事件错误地以空值触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一种奇怪的行为,我无法隔离准确重现它的步骤;但是,在使用小x"重置输入后,它似乎经常发生.日期重置后,用键盘输入有效日期,然后继续切换各种月/日/年设置.

This is odd behavior that I haven't been able to isolate the steps to reproduce it exactly; however, it seems to occur quite frequently after using the little 'x' to reset the input. After the date is reset, enter a valid date with the keyboard and then continue to toggle the various month/day/year settings.

<input id="pi-start" type="date" data-clear-btn="false" value="">

$('#pi-start').on('input', function()
{
    var value = $('#pi-start').val();
    if ( value != '' )
    {
        console.log(value);
    }
    else
    {
        console.log("Blank");
    }
});

您可以在控制台中看到更改月份触发了输入事件,但未设置输入值.

You can see in the console where changing the month fired the input event but the value for the input wasn't set.

这里发生了什么,防止这种情况的最佳方法是什么?我希望能够检测到值的变化.

What's going on here and what's the best way to prevent this? I want to be able to detect the change in value.

Chrome:版本 64.0.3282.119(官方版本)(64 位)

Chrome: Version 64.0.3282.119 (Official Build) (64-bit)

推荐答案

当然,一旦我发布了问题,我就知道发生了什么.为输入触发空值是因为键盘允许您选择无效的日期.IE.2 月 30 日.

Of course once I post the question I figured out what is occurring. The empty value is fired for the input because the keyboard allows you to select a date that is not valid. ie. Feb, 30.

这篇关于html5 输入类型日期输入事件错误地以空值触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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