js input框输入信息 半角转全角

function ToDBC(obj) {
    var str = obj.value;
    var tmp = "";
    for (var i = 0; i < str.length; i++) {
        if (str.charCodeAt(i) == 32) { //空格 32
            tmp = tmp + String.fromCharCode(12288);
        }else if (str.charCodeAt(i) < 127) { //其他半角33 - 127  全角65281-65374     半角全角相差65248
            tmp = tmp + String.fromCharCode(str.charCodeAt(i) + 65248);
        }else{                               //已经转全角过的半角
            tmp = tmp + String.fromCharCode(str.charCodeAt(i));
        }
    }
    obj.value = tmp;
}

 <input id="xxxxx" type="text" onkeyup="ToDBC(this);"/>

 

posted @ 2021-12-22 09:37  徘徊在人间的水墨  阅读(204)  评论(0编辑  收藏  举报