Jelly Bean WebView与文本框的HTML maxlength属性不兼容

Rah*_*ole 13 android maxlength webview android-4.2-jelly-bean

Jelly Bean似乎不喜欢maxlength用于文本输入的HTML属性.它肯定会限制输入字符的数量,但是当您尝试键入超出允许的字符数时,文本框将失败.现在,您将无法键入任何其他文本框,也无法在该文本框中删除已输入的字符.

如果您还没有遇到过这种情况,那么请在简单的HTML上自行尝试并检查.请告诉我你是否有解决这个问题的线索.

Kev*_*lde 20

我在我的应用程序中也遇到了同样的问题

现在我用js处理它,它从输入文本和textarea中删除所有maxlength属性,并阻止用户输入超过所需文本.这里假设所有输入文本和textarea都具有唯一ID.

代码也可以在jsfiddle获得

    $(document).ready(function () {

        var ver = window.navigator.appVersion;
            ver = ver.toLowerCase();

        if ( ver.indexOf("android 4.1") >= 0 ){            

            var idMaxLengthMap = {};

            //loop through all input-text and textarea element
            $.each($(':text, textarea, :password'), function () {
                var id = $(this).attr('id'),
                    maxlength = $(this).attr('maxlength');

                //element should have id and maxlength attribute
                if ((typeof id !== 'undefined') && (typeof maxlength !== 'undefined')) {
                    idMaxLengthMap[id] = maxlength;

                    //remove maxlength attribute from element
                    $(this).removeAttr('maxlength');

                    //replace maxlength attribute with onkeypress event
                    $(this).attr('onkeypress','if(this.value.length >= maxlength ) return false;');
                }
            });

            //bind onchange & onkeyup events
            //This events prevents user from pasting text with length more then maxlength
            $(':text, textarea, :password').bind('change keyup', function () {
                var id = $(this).attr('id'),
                    maxlength = '';
                if (typeof id !== 'undefined' && idMaxLengthMap.hasOwnProperty(id)) {
                    maxlength = idMaxLengthMap[id];
                    if ($(this).val().length > maxlength) {

                        //remove extra text which is more then maxlength
                        $(this).val($(this).val().slice(0, maxlength));
                    }
                }
            });
        }
    });?
Run Code Online (Sandbox Code Playgroud)

此问题的错误已在35264打开


Rah*_*ole 6

如果你的情况非常简单,那么html行中的简单添加也可以起作用,如下所示:

?<input type="text" class="abc" onkeypress="if(this.value.length > 9) return false;"/>??????????????????????????????????????????????????????????????????????????????????????????
Run Code Online (Sandbox Code Playgroud)