2017年11月18日 星期六

網頁輸入頁面Enter鍵處理元件(KeyDownProcess Version:1.65)

function KeyDownProcess(e) {
    //-----------------------------------------------------------
    //網頁親和性輸入控制元件
    //元件功能:可以讓網頁資料的輸入更親切,功能如下:
    //1.按下Enter鍵自動找到下一個可輸入輸入元件
    //2.按下Enter鍵自動找尋submit按鍵,如果找到則模擬Click的功能
    //3.當輸入元件設定唯讀時,按下backspace鍵時,防止網頁轉址
    //4.支援無障礙導覽設定
    //5.支援Firefox、Google Chrome瀏覽器
    //6.支援按下shift鍵全域變數ShiftKeyDown(可實作連選)
    //7.支援頁面元件已輸入資料偵測(全域變數:CursorUserChanged CursorUserChangedDetect 可實作window.onbeforeunload confirm)
    //版本:1.65(2016.11.18)-Microsoft Visual Studio 2017 調校後版本(V1.00)
    //作者:科碩資訊有限公司
    //引用本元件,請保留作者和版本資訊
    //連絡mail:cursor@cursorinfo.com.tw
    //------------------------------------------------------------
    w3cEvent = e;
    ShiftKeyDown = false;
    _beforeunloadTarget = '';
    CursorUserChanged = false;
    var kv = 0;
    var rv = false;
    var isTextarea = false;
    var currentId = null; //目前元件
    var ex;
    var isAll = typeof (document.all) === "object";
    if (isAll) {
        ShiftKeyDown = event.shiftKey;
        ex = event;
        kv = ex.keyCode;
        isTextarea = (ex.srcElement.tagName === "TEXTAREA");
        currentId = ex.srcElement.id === undefined ? '' : ex.srcElement.id;
    }
    else {
        ShiftKeyDown = e.shiftKey;
        ex = e;
        kv = ex.which * 1;
        isTextarea = (ex.target === "[object HTMLTextAreaElement]");
        currentId = ex.target.id === undefined ? '' : ex.target.id;
    }
    switch (kv) {
        case 13:
            if (ex.srcElement.tagName === "A" || (ex.srcElement.tagName === "INPUT" && ex.srcElement.type === "BUTTON"))
                return true;
            if ((currentId.length === 0))
                return false;
            if (isTextarea === true) {
                rv = isTextarea;
            }
            else {
                var cts = isAll ? document.all : document.getElementsByTagName('*');
                var cid = "";
                var queryBu = null;
                var beginNext = false; //開始比對是否為輸入元件
                var nextInput = null; //下一個輸入元件
                var specialSubmitId = "textQueryBU"; //指定特例的submit id
                var exceptionSubmitId = "waitting"; //指定例外的submit Id
                for (ii = 0; ii < cts.length; ii++) {
                    if (cts[ii].type === null || cts[ii].id === null)
                        continue;
                    if (cts[ii].id === currentId) {
                        beginNext = true;
                        continue;
                    }
                    if (beginNext === true && (cts[ii].id.indexOf(specialSubmitId) > -1 || ((cts[ii].type === "submit" || cts[ii].type === "image") && cts[ii].id.indexOf(exceptionSubmitId) === -1))) {
                        queryBu = cts[ii]; //找到submit元件
                        break;
                    }
                    else if (beginNext === true && (cts[ii].type === "text" || cts[ii].type === "textarea" || cts[ii].type === "password") && cts[ii].readOnly === false && cts[ii].style.display !== "none") {
                        nextInput = cts[ii]; //找到下一個文字輸入元件
                        break;
                    }
                }
                if (null !== queryBu) {
                    setTimeout("doClickId('" + queryBu.id + "');", 100);
                    rv = false;
                }
                else if (null !== nextInput && nextInput.offsetWidth !== 0) {
                    try {
                        nextInput.focus();
                    } catch (Error) { return true; }
                    rv = false;
                }
            }
            break;
        case 8:
            if (currentId.length > 0)
                return !document.getElementById(currentId).readOnly;
            else
                return true;
            break;
        default:
            rv = true;
            CursorUserChanged = typeof CursorUserChangedDetect === 'undefined' ? false : CursorUserChangedDetect;
            break;
    }
    return rv;
}
//找到第一個可輸入元件,並設定focus
function getFirstInputControl() {
    var inputs = document.all && typeof (document.all) === "object" ? document.all : document.getElementsByTagName('input');
    for (i = 0; i < inputs.length; i++) {
        if ((inputs[i].type === "text" || inputs[i].type === "textarea") && inputs[i].readOnly === false && inputs[i].offsetWidth !== 0) {
            try { inputs[i].focus(); } catch (error) { }
            break;
        }
    }
}

沒有留言: