0

我正在使用自定义验证器和 jquery 验证复选框列表来检查所需的验证,它的验证很好,但它没有关注错误。

CheckBoxList 和 CustomValidator 的 asp.net 是:

<asp:CheckBoxList ID="cblSellerCategories" 
 runat="server" 
 RepeatDirection="Horizontal"> 
</asp:CheckBoxList>


<asp:CustomValidator ID="CustomValidator2" 
 runat="server"
 ClientValidationFunction="CheckSellerCategory"
 CssClass="errorBox" 
 ErrorMessage="Select seller type" 
 SetFocusOnError="True"> 
</asp:CustomValidator>

而jQuery是

function CheckSellerCategory(sender, args) {
args.IsValid = false;
$("[id$='cblSellerCategories']").find(":checkbox").each(function () {
    if (jQuery(this).attr("checked")) {
        args.IsValid = true;
        return;
    }
});
}

如何关注错误(如果未选中任何复选框)。我也尝试过验证组,但没有运气。

4

1 回答 1

4

如果SetFocusOnError不工作,你可以手动设置焦点做这样的事情

if (!args.IsValid)
   $("[id$='cblSellerCategories'] :checkbox:first").focus();

所以你的代码是

function CheckSellerCategory(sender, args) {
    args.IsValid = false;
    $("[id$='cblSellerCategories']").find(":checkbox").each(function () {
        if (jQuery(this).attr("checked")) {
            args.IsValid = true;
            return;
        }
    });
    if (!args.IsValid)
       $("[id$='cblSellerCategories'] :checkbox:first").focus();        
}
于 2012-10-16T12:35:46.747 回答