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

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

问题描述

概述

我正在编写一个jQuery插件,将一个文本框转换为时间输入框。意思是允许用户以HH:MM格式输入时间。这个过程的一部分是在正确的地方插入一个冒号(:)。在键盘上,我检查用户是否输入了两个数字,如果是,则将冒号附加到输入的值。

I'm writing a jquery plugin which converts a textbox into a time entry box. Meaning it allows the user to enter time in HH:MM format. Part of this process is to insert a colon (:) at the right place. On keyup i check if the user has typed 2 numbers and if so append a colon to the entered value.

问题

这种方法非常有效,除非有人在文本框上应用插件,但也希望有一个自定义onchange处理程序。当代码以编程方式更新文本框值时,此onchange处理程序不会触发

This approach works fine except when someone applies the plugin on the textbox but also wants to have a custom onchange handler.This onchange handler does not fire when the code updates the textbox value programmatically nor does it fire when the textbox loses focus.

我的理解:

在这里猜测,所以如果我错了,请纠正我。

I'm guessing here, so please correct me if I'm wrong.


  • 也许文本框有一个内部的_ (强调
    与公共价值属性区别开来)
    字段,它在编辑开始之前保存文本框中的
    值。当焦点离开
    texbox时,JS引擎检查文本框
    的当前值是否与文本框的_ value匹配。如果它们不同的是,
    onchange 事件被触发。

  • 也许使用常规的本机击键值更改不要修改
    _ value只是这个值,所以 onchange 被触发。但是可能通过JS修改
    值更改两者的值和_ value
    ,因此onchange事件不起火?

  • Maybe the textbox has an internal "_value" (underscoring to differentiate from the public value attribute) field which holds the value in the textbox before the edit begins. When the focus leaves the texbox, the JS engine checks if the current value of the textbox matches the "_value" of the textbox.If they are different, the onchange event is fired.
  • Perhaps value changes using regular native keystrokes dont modify "_value" just the value, so the onchange gets fired. But perhaps value changes made via JS modify both the value and the "_value" and so the onchange event does not fire?

替代方法:

模糊事件触发,因为它与该值无关。所以我可以明确地从模糊事件触发变化事件。但是我不想这样做,因为它是一个黑客,打破了模糊和改变的语义。

The blur event fires as it has nothing to with the value. So I can explicitly fire the change event from the blur event.But I dont want to do this as it is a hack and breaks the semantics of blur and change.

相关链接

我在SO上找到了相同的问题的几个链接,但没有回答(IMHO)提供了一个优雅或正确的解决方案。
这里是我看到的一些:

I found several links on SO with the same question but no answer (IMHO) gives an elegant or correct solution. here are some of the ones I saw:

  • Javascript : onchange event does not fire when value is changed in onkeyup event

https://stackoverflow.com/questions/14410870/using-both-onchange-and-onkeyup-in-ie8

Javascript:onchange事件不适用于价值 文本输入中的改变对象

简化代码

$(document).ready(function () {
    $("input").keyup(function () {
        this.value = '*' + this.value;
        // this changes the value, but does not fire the onchange event
        // and it also does not fire it when the textbox loses focus.
        // blur fires though.
    });
    $("input").change(function () {
        alert('in change');
    });

    $("input").blur(function () {
        alert('in blur');
    });
});

FIDDLE:

http://jsfiddle.net/sajjansarkar/vHgst/4/

浏览器测试:
IE8,10和Chrome。
在FireFox(!!)(+1 Felix Kling)中工作

BROWSERS TESTED: IE8,10 and Chrome. Works in FireFox(!!) (+1 Felix Kling)

推荐答案

您可以使用jquery以编程方式触发事件。触发器:
http://jsfiddle.net/vHgst/5/

You can trigger events programmatically with jquery.trigger : http://jsfiddle.net/vHgst/5/

$(this).trigger('change');

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

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