Jelly Bean WebView 不能与文本框的 HTML maxlength 属性配合使用 [英] Jelly Bean WebView not working well with HTML maxlength attribute for text box

查看:20
本文介绍了Jelly Bean WebView 不能与文本框的 HTML maxlength 属性配合使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Jelly Bean doesn't seem to like the maxlength attribute of HTML for text input. It certainly restricts the number of input characters but the moment you attempt to type beyond the allowed number of character, the text box fails. Now you won't be able to type in any other text boxes and nor will you be able to delete the already input character, in that text box.

如果您还没有遇到过这种情况,请在简单的 HTML 上亲自尝试并检查.如果您有解决此问题的任何线索,请告诉我.

If you haven't already faced this, then try it yourself on a simple HTML and check. Please tell me if you have any clue for solving this.

推荐答案

我的应用也遇到了同样的问题

I have also experienced the same problem in my app

现在我已经用 js 处理了它,它从输入文本和 textarea 中删除了所有 maxlength 属性,并阻止用户输入超过所需文本的内容.这里假设所有输入的文本和文本区域都有唯一的 id.

for now I have handled it with js, which removes all maxlength attributes from input text and textarea and stops user from inputing more than the required text. Here it is assumed that all input text and textarea have unique 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));
                    }
                }
            });
        }
    });​

此问题的错误已在 35264

这篇关于Jelly Bean WebView 不能与文本框的 HTML maxlength 属性配合使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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