简体   繁体   English

长度未返回正确的值

[英]Length not returning the correct value

I have the following code: 我有以下代码:

$(document).ready(function (e) {

    $('#input').keydown(function (e)
    {
        var inpData_lenght = $(this).val().length+1;
        var max_length     = 15;
        if (inpData_lenght >= max_length)
        {
            if (inpData_lenght > max_length)
            {
                if ((e.keyCode != 8) && (e.keyCode != 46))
                {
                    e.stopImmediatePropagation();
                    e.preventDefault();
                }
            }

            $(this).css({'background-color': '#ff8080'});
        }
        else
        {
            $(this).css('background-color', '');
        }  
    });
});

HTML: HTML:

<textarea id="input"></textarea>

(fiddle) (小提琴)

The textarea does not change the background back to white when there are max_length-1 characters but only on max_length-3 , is there any workaround? 当有max_length-1字符时,textarea不会将背景变回白色,而只有在max_length-3 ,有任何解决方法吗? I am aware it has to do with the backspace or delete that is counting as character. 我知道这与退格或删除算作字符有关。

EDIT: I can only use keydown because that is the only key related callback the plugin supports. 编辑:我只能使用keydown,因为这是插件支持的唯一与密钥相关的回调。

It's better to use keyup() instead of keydown() because you will get the resulting input value. 最好使用keyup()而不是keydown()因为您将获得结果输入值。 If the length exceeds maximum just replace the input value to the previous one. 如果长度超过最大值,只需将输入值替换为前一个值即可。 http://jsfiddle.net/9Ed8h/ http://jsfiddle.net/9Ed8h/

$(document).ready(function (e) {

    $('#input').keyup(function (e)
    {
        var inpData_lenght = $(this).val().length;
        var max_length     = 15;
        if (inpData_lenght >= max_length)
        {
            if (inpData_lenght > max_length)
            {
                $(this).val(this.lastValue);
            }

            $(this).css({'background-color': '#ff8080'});
        }
        else
        {
            $(this).css('background-color', '');
        }  
        this.lastValue = $(this).val();
    });
});

Using keyup will solve your problem. 使用keyup将解决您的问题。 You should think of a way to prevent people holding their key down. 您应该想出一种方法来防止人们按下钥匙。 (If you hold a key for 2 seconds, your limit of 15 chars will be exceeded) (如果您按住某个键2秒钟,则将超出15个字符的限制)

How to disable repetitive keydown in jQuery 如何在jQuery中禁用重复的keydown

Combine the maxlength and keyup should be the easiest way. 合并maxlength和keyup应该是最简单的方法。

<textarea id="input" maxlength=3></textarea>

And

$(document).ready(function (e) {

    $('#input').keyup(function (e)
    {
        var inpData_lenght = $(this).val().length;
        var max_length     = 3;
        if (inpData_lenght >= max_length)
        {
            $(this).css({'background-color': '#ff8080'});
        }
        else
        {
            $(this).css('background-color', '');
        }  
    });
});

See the fiddle: http://jsfiddle.net/DEjgd/4/ 参见小提琴: http : //jsfiddle.net/DEjgd/4/

If you need a workaround for keydown , the following should work: 如果您需要解决keydown的方法,则应该可以使用以下方法:

var maxLength = 15;
var currLength = this.value.length;
if (e.keyCode == 8 || e.keyCode == 46) {
    var expectedLength = currLength - 1; // distinguish here!
} else {
    var expectedLength = currLength + 1;
    if (expectedLength > maxLength) {
         e.stopImmediatePropagation();
         e.preventDefault();
    }
}
if (expectedLength >= maxLength)  {
    $(this).css('background-color', '#ff8080');
} else {
    $(this).css('background-color', '');
}  

除了使用JavaScript条件之外,您还可以使用类似这样的内容

<textarea maxlength="15" id="input"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM