如何更改必需字段验证程序的SetFocusOnError属性的默认工作

时间:2013-10-12 13:44:52

标签: javascript jquery asp.net

setFocusOnError = true的默认行为将焦点设置在错误字段上。但问题是窗户顶部有一个有固定位置的横幅。由于哪个横幅隐藏了错误字段。如何覆盖setFocusOnError的默认工作,以便横幅和错误字段之间有余量?

enter image description here

1 个答案:

答案 0 :(得分:0)

<script type="text/jscript">
        $(function () {
            // on first time page load
            if (typeof (Page_ClientValidate) != "undefined") {
                ValidatorSetFocus = CustomSetFocus;
            }
        });

        function CustomSetFocus(val, event) {

            var ctrl;
            if (typeof (val.controlhookup) == "string") {
                var eventCtrl;
                if ((typeof (event) != "undefined") && (event != null)) {
                    if ((typeof (event.srcElement) != "undefined") && (event.srcElement != null)) {
                        eventCtrl = event.srcElement;
                    }
                    else {
                        eventCtrl = event.target;
                    }
                }
                if ((typeof (eventCtrl) != "undefined") && (eventCtrl != null) &&
            (typeof (eventCtrl.id) == "string") &&
            (eventCtrl.id == val.controlhookup)) {
                    ctrl = eventCtrl;
                }
            }
            if ((typeof (ctrl) == "undefined") || (ctrl == null)) {
                ctrl = document.getElementById(val.controltovalidate);
            }
            if ((typeof (ctrl) != "undefined") && (ctrl != null) &&
        (ctrl.tagName.toLowerCase() != "table" || (typeof (event) == "undefined") || (event == null)) &&
        ((ctrl.tagName.toLowerCase() != "input") || (ctrl.type.toLowerCase() != "hidden")) &&
        (typeof (ctrl.disabled) == "undefined" || ctrl.disabled == null || ctrl.disabled == false) &&
        (typeof (ctrl.visible) == "undefined" || ctrl.visible == null || ctrl.visible != false) &&
        (IsInVisibleContainer(ctrl))) {
                if ((ctrl.tagName.toLowerCase() == "table" && (typeof (__nonMSDOMBrowser) == "undefined" || __nonMSDOMBrowser)) ||
            (ctrl.tagName.toLowerCase() == "span")) {
                    var inputElements = ctrl.getElementsByTagName("input");
                    var lastInputElement = inputElements[inputElements.length - 1];
                    if (lastInputElement != null) {
                        ctrl = lastInputElement;
                    }
                }
                if (typeof (ctrl.focus) != "undefined" && ctrl.focus != null) {
                    ctrl.focus();
                    Page_InvalidControlToBeFocused = ctrl;
                    var temp1=$(window).scrollTop();
                    temp1 = temp1 - 150;
                    $(window).scrollTop(temp1);
                }
            }

        }
    </script>