在 Chrome 中使用 jquery 的文本输入时未触发更改事件

发布于 2024-12-20 03:42:25 字数 507 浏览 5 评论 0原文

我在文本输入上遇到 jquery 更改事件问题,该事件在 Firefox 和 IE 中按预期工作,但在 Chrome 中却不行。我还在文本输入上有一个 keyup 事件,用于在输入时操作输入,但是当输入完成后,我需要运行一些额外的代码。我尝试使用“模糊”事件和“焦点”事件作为替代品,就像这里有人建议的那样,但后来我根本无法改变焦点—​​—输入总是以某种方式将其夺回。这是代码:

 $('.textInput').keyup(function(event) { /* do stuff */ });
 $('.textInput').change(function(event) { alert('Changed!'); /* do other stuff */ });

和 html:

 <input type="text" class="textInput" value="" />

我正在使用 jQuery 1.6.3。任何建议将不胜感激。 H.

I have a problem with a jquery change event on a text input that works as expected in Firefox and IE but not in Chrome. I also have a keyup event on the text input to manipulate the input as it is entered, but when the input is done I need to run some extra code. I tried using the 'blur' event and the 'focusout' event as someone here had suggested as a substitute, but then I couldn't change focus at all -- the input kept grabbing it back somehow. Here is the code:

 $('.textInput').keyup(function(event) { /* do stuff */ });
 $('.textInput').change(function(event) { alert('Changed!'); /* do other stuff */ });

and the html:

 <input type="text" class="textInput" value="" />

I am using jQuery 1.6.3. Any suggestions would be greatly appreciated.
H.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

格子衫的從容 2024-12-27 03:42:25

除非输入焦点切换到其他控件,否则更改事件不会触发

The change event will not fire unless the input focus switched to other controls

遗心遗梦遗幸福 2024-12-27 03:42:25

您可以使用像超级按钮一样工作的 input 事件:

$('#search-form .term').bind('input', function(){
  console.log('this actually works');
});

来源:https:// gist.github.com/brandonaaskov/1596867

You can use the input event that works like a charm:

$('#search-form .term').bind('input', function(){
  console.log('this actually works');
});

Source: https://gist.github.com/brandonaaskov/1596867

泪眸﹌ 2024-12-27 03:42:25
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="Scripts/jquery-1.7.1.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $('.textInput').keyup(function (event) {
                var obj = $(this);
                if ((event.keyCode ? event.keyCode : event.which) === 13) {
                    $('#result').text('Enter has been triggered.');
                }
            }); ;

            $('.textInput').change(function (event) {
                var obj = $(this);
                $('#result').text('Input text has been changed:' + obj.val());
            }); ;
        });
    </script>
</head>
<body>
    <span id="result"></span>
    <br />
    Enter some text and press enter:
    <input type="text" class="textInput" value="" />
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="Scripts/jquery-1.7.1.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $('.textInput').keyup(function (event) {
                var obj = $(this);
                if ((event.keyCode ? event.keyCode : event.which) === 13) {
                    $('#result').text('Enter has been triggered.');
                }
            }); ;

            $('.textInput').change(function (event) {
                var obj = $(this);
                $('#result').text('Input text has been changed:' + obj.val());
            }); ;
        });
    </script>
</head>
<body>
    <span id="result"></span>
    <br />
    Enter some text and press enter:
    <input type="text" class="textInput" value="" />
</body>
</html>
私野 2024-12-27 03:42:25

实际上,Google Chrome 上有一个错误,如果“keyup”事件更改了某些内容,则导致“更改”事件不会触发:

https://code.google.com/p/chromium/issues/detail?id=92492

该问题似乎自 5 月 2 日起就已开放, 2013年。

Actually, there's a bug on Google Chrome that causes the 'change' event to not fire if something has been changed by the 'keyup' event:

https://code.google.com/p/chromium/issues/detail?id=92492

The issue seems to be open since May 2, 2013.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文