Javascript onchange 日期输入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5633412/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 18:10:33  来源:igfitidea点击:

onchange on date inputs

javascriptcsshtmlinputonchange

提问by tbleckert

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

可能的重复:
当 <input type="number" /> 值改变时会触发什么事件?

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?

在 webkit 和 firefox 中,它会在输入端添加两个向上/向下箭头。单击它们时,日期会向前或向后移动,但直到该字段失去焦点时才会触发 onchange 事件。有解决方法吗?

回答by mVChr

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

您可以将该值存储在全局中并检查 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 oninputevent)

编辑:或者,重复的问题,就像评论建议的那样。(见oninput活动)