jQuery验证软编码文本框名称

时间:2010-08-25 14:08:05

标签: jquery jquery-validate

$("#commentForm").validate({
            rules: {
                insurance1$ucBuildingInsurance$txtOtherReasonDescription: required
}
});

我当前的脚本看起来像验证文本框txtOtherReasonDescription.Ideally我想用$()语法替换insurance1 $ ucBuildingInsurance $ txtOtherReasonDescription,因此控件ID不是硬编码的。无论如何这可以实现吗?

1 个答案:

答案 0 :(得分:1)

您可以使用如下选择器添加规则:

$("#commentForm").validate();
$("input[id$='txtOtherReasonDescription']").rules("add", { required: true });

由于您在ASP.Net中,id$=name$=在这里工作,但概念在表单上设置了valdiation,然后使用{{{}将规则附加到匹配的元素3}}