Javascript:在onkeyup事件中更改值时,onfire事件不会触发 [英] Javascript : onchange event does not fire when value is changed in onkeyup event

查看:101
本文介绍了Javascript:在onkeyup事件中更改值时,onfire事件不会触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个简单的代码:

    <body>
        <input type="text" onkeyup="this.value = (new Date()).getSeconds()" onchange="alert(1)" /> 
    </body>

当我按下键但是onchange事件确实时,我不知道为什么文本框值已经改变了不开火。
如何触发onchange事件?

I dont't know why the textbox value has changed when i press the key but the onchange event does not fire. How can i fire onchange event ?

推荐答案

onchange 当您离开焦点时将触发(如果您执行了任何更改)。只有 text textarea 输入类型才会出现此问题,因为javascript不知道您何时完成输入。

The onchange will fire when you leave the focus from it (if you performed any changes). This behavior occurs only with text and textarea input types because javascript doesn't know when you're done typing.

我不知道为什么代码中有复选框,但无论如何。真诚的,我无法告诉你原因,但问题是输入的动态值变化(猜测是用户不会执行更改,但我真的不知道),如果你删除onkeyup中的代码它将工作。无论如何,如果您想要的是在输入失去焦点时执行某些代码,您可以使用 onblur ,在这种情况下更准确。

I don't know why there is a checkbox in the code, but whatever. Sincerely, I'm not being able to tell you why, but the problem is the dynamic value change on the input (a guess would be that the change isn't performed by the user, but I really don't know), if you remove the code inside the onkeyup it will work. Whatever, if what you want is to execute some code when the input loses focus, you can use onblur instead, is more accurate in that case.

这篇关于Javascript:在onkeyup事件中更改值时,onfire事件不会触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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