输入元素上的javascript更改事件仅在失去焦点时触发 [英] javascript change event on input element fires on only losing focus

查看:262
本文介绍了输入元素上的javascript更改事件仅在失去焦点时触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个输入元素,我想继续检查内容的长度,每当长度变得等于特定大小时,我想启用提交按钮,但是我遇到了javascript onchange事件的问题因为事件仅在输入元素超出范围时触发,而不是在内容更改时触发。

I have an input element and I want to keep checking the length of the contents and whenever the length becomes equal to a particular size, I want to enable the submit button, but I am facing a problem with the onchange event of javascript as the event fires only when the input element goes out of scope and not when the contents change.

< input type =textid =nameonchange =checkLength(this.value)/>

---- onchange不会因更改内容而触发名称,但只有在名称失焦时才会触发。

----onchange does not fire on changing contents of name, but only fires when name goes out of focus.

我可以做些什么来使这个事件能够改变内容?还是我可以使用的其他事件?
我通过使用onkeyup函数找到了一种解决方法,但是当我们从浏览器的自动完成程序中选择一些内容时,这不会触发。

Is there something I can do to make this event work on content change? or someother event I can use for this? I found a workaround by using the onkeyup function, but that does not fire when we select some content from the auto completer of the browser.

我想要一些东西无论是通过键盘还是通过鼠标来改变字段的内容都可以工作...任何想法?

I want something which can work when the content of the field change whether by keyboard or by mouse...any ideas?

推荐答案

(function () {
    var oldVal;

    $('#name').on('change textInput input', function () {
        var val = this.value;
        if (val !== oldVal) {
            oldVal = val;
            checkLength(val);
        }
    });
}());

这将抓住更改,按键,粘贴 textInput 输入(如果可用)。并且不要超过必要的火力。

This will catch change, keystrokes, paste, textInput, input (when available). And not fire more than necessary.

http:// jsfiddle.net/katspaugh/xqeDj/

参考文献:

textInput - W3C DOM Level 3事件类型。 http://www.w3.org/TR/DOM- Level-3-Events /#events-textevents


当一个或多个字符有$时,用户代理必须发送此事件b $ b已输入。这些字符可能来自各种
源,例如,键盘设备上按下的键或
释放的字符,来自输入法
编辑器的处理,或者来自语音命令。如果粘贴操作
生成一个简单的字符序列,即没有任何结构或样式信息的文本段落
,则此事件类型也应为
生成。

A user agent must dispatch this event when one or more characters have been entered. These characters may originate from a variety of sources, e.g., characters resulting from a key being pressed or released on a keyboard device, from the processing of an input method editor, or resulting from a voice command. Where a "paste" operation generates a simple sequence of characters, i.e., a text passage without any structure or style information, this event type should be generated as well.

输入 - HTML5 事件类型。

input — an HTML5 event type.


当用户更改值时在控件处触发

Fired at controls when the user changes the value

Firefox,Chrome, IE9 和其他现代浏览器都支持它。

Firefox, Chrome, IE9 and other modern browsers support it.


此事件在修改后立即发生,与onchange事件不同,后者发生在元素失去焦点时。

This event occurs immediately after modification, unlike the onchange event, which occurs when the element loses focus.

这篇关于输入元素上的javascript更改事件仅在失去焦点时触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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