禁用基于单选按钮检查的必选字段验证器

Disable Required Field Validator based on RadioButton Check

本文关键字:字段 验证 检查 单选按钮      更新时间:2023-09-26

当单选按钮值==2时,代码应该启用false。但每次我点击提交按钮,它提示我的RequiredFieldValidator的LastName文本框。

谢谢!

 $('#<%= rblIndOrg.ClientID %>').change(function() { 
                if ($('#<%= rblIndOrg.ClientID %> input:checked').val() == "2") { 
                    $('#<%= tbLast_Name.ClientID%>').val(""); 
                    $('#<%= tbLast_Name.ClientID %>').attr("style", "display: none;");
                    $('#lblLast_Name').attr("style", "display: none;"); 
                    document.getElementById("<%=rfv_tbLast_Name.ClientID%>").enabled = false; 
                } 
                else { 
                    $('#<%= tbLast_Name.ClientID %>').attr("style", "display:inline;"); 
                    $('#lblLast_Name').attr("style", "display: block;"); 
                    document.getElementById("<%=rfv_tbLast_Name.ClientID%>").enabled = true; 
                    }
            }); 

不仅要禁用tbLast_Name,还要删除required-attribute(如果存在)

$('#lblLast_Name').attr("style", "display: none;"); 
$('#lblLast_Name').removeAttr('required');

在else块中,您应该再次在第一行添加required属性。