在samsung S5中,maxlength属性不起作用

时间:2015-10-29 07:05:04

标签: javascript html5 samsung-mobile

html 5 maxlength不适用于三星设备。如何限制用户使用javascript在文本框中输入超过最大字符数。

1 个答案:

答案 0 :(得分:2)

如果长度有效,您可以检查keyup事件......

old_value = "";

document.querySelector(yourinput).addEventListener("keyup", function(e){
    if (this.value.length > 20){
        //set the old value
        this.value == old_value;
    }
    else{
        old_value = this.value;
    }
})

未经测试只是了解如何处理此问题。

OR (我认为这不会奏效,但很高兴知道)

document.querySelector(yourinput).maxLength = "20";
//ie: document.querySelector(yourinput).setAttribute("maxLength", 20);
相关问题