CustomValidator 的 SetFocusOnError 不起作用

发布于 2024-12-20 04:16:35 字数 1289 浏览 3 评论 0原文

我有一些带有 CustomValidators 的控件,如下所示。但是 SetFocusOnError 不起作用,如果验证失败,页面不会跳转到第一个控件。我可以在javascript中设置焦点,但问题是最后一个无效的控件总是获得焦点,这不是我想要的,我想要第一个无效的控件获得焦点。

谁能告诉我如何解决这个问题?谢谢。

function ValidateRootCause(source, args) {
    var status = document.getElementById("<%=statusDropDownList.ClientID %>");
    var RootCause = document.getElementById("<%=txtRootCause.ClientID %>");

    args.IsValid = true;
    if (RootCause.value == "" && (status.value == 21 || status.value == 18)) {
        args.IsValid = false;
        RootCause.focus();
    }
}

        <tr>
            <td width="45%">Root Cause (Why it Happens)<span class="littlefont">*</span>                
            <asp:CustomValidator ID="cvRootCause" runat="server" 
                    ErrorMessage="Required Field!" CssClass="warning" 
                    ClientValidationFunction="ValidateRootCause" ValidationGroup="formValidation" SetFocusOnError="True">
            </asp:CustomValidator>
            </td>
            <td width="55%">

                <asp:TextBox ID="txtRootCause" runat="server" TextMode="MultiLine" 
                    MaxLength="1000"></asp:TextBox>
            </td>
        </tr>

I have some controls with CustomValidators like following. However the SetFocusOnError is not working, the page doesn't jump to the first control if validation fails. I could set focus in the javascript, but the problem with it is that always the last control that is not valid gets the focus, which is not what I want, I want the first control that is not valid gets the focus.

Can anyone tell me how to fix this? Thanks.

function ValidateRootCause(source, args) {
    var status = document.getElementById("<%=statusDropDownList.ClientID %>");
    var RootCause = document.getElementById("<%=txtRootCause.ClientID %>");

    args.IsValid = true;
    if (RootCause.value == "" && (status.value == 21 || status.value == 18)) {
        args.IsValid = false;
        RootCause.focus();
    }
}

        <tr>
            <td width="45%">Root Cause (Why it Happens)<span class="littlefont">*</span>                
            <asp:CustomValidator ID="cvRootCause" runat="server" 
                    ErrorMessage="Required Field!" CssClass="warning" 
                    ClientValidationFunction="ValidateRootCause" ValidationGroup="formValidation" SetFocusOnError="True">
            </asp:CustomValidator>
            </td>
            <td width="55%">

                <asp:TextBox ID="txtRootCause" runat="server" TextMode="MultiLine" 
                    MaxLength="1000"></asp:TextBox>
            </td>
        </tr>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猫九 2024-12-27 04:16:35

创建一个名为“focusGiven”或类似名称的全局(在函数之外)JavaScript 变量。在 if() 中,在焦点调用周围包装另一个 if() 检查。这样它只会发生一次。

var focusGiven = false;
function ValidateRootCause(source, args) {
    var status = document.getElementById("<%=statusDropDownList.ClientID %>");
    var RootCause = document.getElementById("<%=txtRootCause.ClientID %>");

    args.IsValid = true;
    if (RootCause.value == "" && (status.value == 21 || status.value == 18)) {
        args.IsValid = false;

        if (!focusGiven) {
            RootCause.focus();
            focusGiven = true;
        }
    }
}

Create a global (outside of your function) JavaScript variable called "focusGiven" or something like that. In your if(), wrap another if() check around your focus call. This way it will only happen once.

var focusGiven = false;
function ValidateRootCause(source, args) {
    var status = document.getElementById("<%=statusDropDownList.ClientID %>");
    var RootCause = document.getElementById("<%=txtRootCause.ClientID %>");

    args.IsValid = true;
    if (RootCause.value == "" && (status.value == 21 || status.value == 18)) {
        args.IsValid = false;

        if (!focusGiven) {
            RootCause.focus();
            focusGiven = true;
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文