setSelectionRange解决办法不为Android 4.0.3工作 [英] setSelectionRange workaround doesn't work for android 4.0.3

查看:305
本文介绍了setSelectionRange解决办法不为Android 4.0.3工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to mask the phone number as the user types. I have used the javascript code below with jquery and the setTimeout workaround successfully on android 2.x devices, but I have not found a workaround for that works for 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();
});

}

推荐答案

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

I just met the same problem. My solution is as follows,

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

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

It works in android 4.0.3 in my HTC.

这篇关于setSelectionRange解决办法不为Android 4.0.3工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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