使用Jquery捕获文本框文本总是“落后一个字符" [英] Text box text capture using Jquery always 'one character behind'

查看:110
本文介绍了使用Jquery捕获文本框文本总是“落后一个字符"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用jquery和keydown事件捕获用户输入.

这是我的代码:

 $(document).ready(function() {
        $("#searchText").keydown(function() {
            var filter = jQuery.trim($(this).val());
            if (filter.length > 3 || filter.length == 0) {
                //hit the index action again and pass the keyword
                $("#results").fadeOut("fast");
                $("#results").load("/Organisation/Index?keyword=" + filter, null, function() {
                    $(this).fadeIn("fast");
                });
            }
        });
    });

在mo上这是可行的,除了以下事实:捕获的字符串似乎总是被一个字符过时",而且我必须按另一个键才能真正获取想要传递给我的动作的文本. /p>

提前谢谢!

解决方案

您遇到的问题是'keydown'事件.由于在按下键时会完成值的处理,因此新输入的字符尚未考虑到输入中.通过使用键",在将新按下的字符添加到值中之后进行处理.

 $(document).ready(function() {
        $("#searchText").keyup(function() {
            var filter = jQuery.trim($(this).val());
            if (filter.length < 3 || filter.length == 0) {
                //hit the index action again and pass the keyword
                $("#results").fadeOut("fast");
                $("#results").load("/Organisation/Index?keyword=" + filter, null, function() {
                    $(this).fadeIn("fast");
                });
            }
        });
    });

I am attempting to capture user input using jquery and the keydown event.

Here's my code:

 $(document).ready(function() {
        $("#searchText").keydown(function() {
            var filter = jQuery.trim($(this).val());
            if (filter.length > 3 || filter.length == 0) {
                //hit the index action again and pass the keyword
                $("#results").fadeOut("fast");
                $("#results").load("/Organisation/Index?keyword=" + filter, null, function() {
                    $(this).fadeIn("fast");
                });
            }
        });
    });

At the mo this is working, aside from the fact that the string captured always seems to be 'out of date' by one character and I have to press another key to actually get the text I want passed to my action.

Thanks in advance!

解决方案

You problem is the 'keydown' event. Since the processing of the value is done when key is press down which the newly pressed character is not yet accounted to the input. By using 'keyup', the processing is done after the newly pressed character is already added to the value.

 $(document).ready(function() {
        $("#searchText").keyup(function() {
            var filter = jQuery.trim($(this).val());
            if (filter.length < 3 || filter.length == 0) {
                //hit the index action again and pass the keyword
                $("#results").fadeOut("fast");
                $("#results").load("/Organisation/Index?keyword=" + filter, null, function() {
                    $(this).fadeIn("fast");
                });
            }
        });
    });

这篇关于使用Jquery捕获文本框文本总是“落后一个字符"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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