setSelectionRange解决方法并不适用于Android 4.0.3工作(setSelec

2019-10-17 03:25发布

我试图掩盖的电话号码作为用户类型。 我用下面jQuery和成功的解决方法的setTimeout在Android 2.x设备的javascript代码,但是我还没有找到一个解决方法的作品为Android 4.0.3。

if (navigator.userAgent.toLowerCase().indexOf("android") >= 0) {
$.fn.usphone = function() {
    this.keyup(function(e) {
        // do not process del, backspace, escape, arrow left and arrow right characters
        var k = e.which;
        if (k == 8 || k == 46 || k == 27 || k == 37 || k == 39)
            return;
        // remove invalid characters
        var value = "";
        for (var i = 0; i < this.value.length; i++) {
            var ch = this.value[i];
            if (ch >= "0" && ch <= "9")
                value += ch;
        }
        // remove extra characters
        if (value.length > 10)
            value = value.substring(0, 10);
        // insert formatting characters
        if (value.length >= 3)
            value = "(" + value.substring(0, 3) + ")" + value.substring(3);
        if (value.length > 5)
            value = value.substring(0, 5) + " " + value.substring(5);
        if (value.length > 9)
            value = value.substring(0, 9) + "-" + value.substring(9);
        // set new value
        var $this = this;
        var length = value.length;
        setTimeout(function() {
            $this.value = value;
            $this.setSelectionRange(length, length);
        }, 0);
    });
};

$('#contact_edit_page, #contact_new_page, #callback_create, #callback_edit, #new_phonecall_contact_page, #new_phonecall').live('pagecreate', function() {
    $('[type^="tel"]').usphone();
});

}

Answer 1:

我刚刚遇到了同样的问题。 我的解决方案如下,

.input {
  -webkit-user-modify: read-write;
}

它可以在安卓4.0.3在我的HTC。



文章来源: setSelectionRange workaround doesn't work for android 4.0.3