work_03_常见jq问题处理

1.获取不同浏览器按下的键码

$(document).ready(function(){
   $("#keydown").keydown(function(event){
        var keycode = (event.keyCod ? event.keyCode : event.which);
        console.log("keycode:" + keycode);
        }
    }) 
})    

2.输入卡号卡号规则***** **** **** ****** 中间有空格

$('#cardno').on('input', function () {
        //去掉所有空格
        this.value = this.value.replace(/\s/g, "");

        if (/^[0-9]*[1-9][0-9]*$/.test(this.value)) {
            var len = this.value.length;
            if (len >= 6 && len <=9) {
                this.value = this.value.substring(0, 5) + " " + this.value.substring(5)
            } else if (len >= 10 && len <=13) {
                this.value = this.value.substring(0, 5) + " " + this.value.substring(5, 9) + " " + this.value.substring(9)
            } else if (len >= 14 && len <=20) {
                this.value = this.value.substring(0, 5) + " " + this.value.substring(5, 9) + " " + this.value.substring(9, 13) + " " + this.value.substring(13)
            }
        }
    })

猜你喜欢

转载自www.cnblogs.com/asndxj/p/12524052.html