html5 input type date input event incorrectly fire

2019-01-29 12:33发布

问题:

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: Version 64.0.3282.119 (Official Build) (64-bit)

回答1:

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.