回车键替代Tab键及提交功能(原生JS)

 window.onload = function(){
        document.getElementById('one').focus();
    }
    var inputGroup = document.getElementsByTagName("input");
    var inputGroupArr = Array.from(document.getElementsByTagName("input"));
    var iGlength = inputGroupArr.length;
    document.onkeypress = function(e){
        var e = event || e;
        console.log(inputGroupArr.indexOf(e.srcElement));
        var idx = inputGroupArr.indexOf(e.srcElement);
        console.log(e,e.keyCode,e.srcElement,e.which);
        if(e.keyCode == 13 || e.which == 13){
            // e.which = 9;
            // e.charCode = 9;
            // e.code = "tab";
            //并不能实现
            if(idx == iGlength - 1){//表明已经是最后一个输入框
                if(confirm('确认提交吗?')){
                    document.getElementsByTagName("form")[0].submit();           
                }
            }else{
                inputGroup[idx+1].focus(); 
            }
            e.preventDefault();
        }
        console.log(e,e.keyCode,e.srcElement,e.which);
    }

 

posted @ 2019-05-22 09:20  林不渡  阅读(828)  评论(0编辑  收藏  举报