onchange on date inputs [duplicate]

2019-07-14 06:45发布

Possible Duplicate:
What events does an <input type=“number” /> fire when it's value is changed?

I have a date input field

<input type="date" name="..">

In webkit and firefox, it will be added two up/down arrows on the side of the input. When clicking them, the date goes forward or backwards, but the onchange event doesn't trigger until the field loses focus. Is there a workaround for this?

1条回答
Evening l夕情丶
2楼-- · 2019-07-14 07:03

You could store the value in a global and check for a difference onclick.

<input type="date" name="myDate" id="myDate" />

var myApp = {};
myApp.myDate = "";

document.getElementById('myDate').onclick = function (e) {
    if (this.value != myApp.myDate) {
        // run onchange code

        // then set last value to current value
        myApp.myDate = this.value;
    }
};

See example →

EDIT: Or, duplicate question, like comments suggest. (See oninput event)

查看更多
登录 后发表回答