简体   繁体   English

如何使用 jquery/javascript 将 asp:RequiredFieldValidator 分配给 asp:TextBox

[英]How to assign asp:RequiredFieldValidator to asp:TextBox with jquery/javascript

Just wondering, will it be possible to assign the request validation to asp:TextBox with jquery/javascript?只是想知道,是否可以使用 jquery/javascript 将请求验证分配给 asp:TextBox?

I have the following code will create a checkbox with a textbox next to it:我有以下代码将创建一个复选框,旁边有一个文本框:

    <tr>   
        <th class="graytext r">Add Reps to Team:</th>
        <td>           
         <asp:GridView ID="grid" runat="server" AutoGenerateColumns="False" DataKeyNames="EmployeeID"
                DataSourceID="dsEmployees" EnableViewState="false"
                GridLines="None" CssClass="clGridDirectory">
                <Columns>
                  <asp:TemplateField >
                    <ItemTemplate>
                      <asp:CheckBox runat="server" ID="employee_name" CssClass="employee_name" Text='<%# Eval("fullname") %>'/> 
                      <asp:HiddenField runat="server" ID="employeeidToRep" Value='<%# Eval("employeeid") %>'/>
                      <asp:TextBox runat="server" ID="repID" Text='<%# Eval("rep_id") %>'  />
                    </ItemTemplate>
                  </asp:TemplateField>
                </Columns>
              </asp:GridView>       
           <asp:SqlDataSource ID="dsEmployees" runat="server" ConnectionString="<%$ ConnectionStrings:TestConnectionString %>"
                SelectCommand="app_staff_without_team_select" SelectCommandType="StoredProcedure">
          </asp:SqlDataSource>        
        </td>
    </tr>   

Just wondering, will it be possible for me to assign the asp:RequiredFieldValidator to the textbox when checkbox is checked or removed the asp:RequiredFieldValidator when checkbox is unchecked?只是想知道,我是否可以在选中复选框时将 asp:RequiredFieldValidator 分配给文本框,或者在未选中复选框时删除 asp:RequiredFieldValidator ?

ASP.NET renders a client-side function named ValidatorEnable that you can use to enable/disable a validator on the fly. ASP.NET 呈现一个名为ValidatorEnable的客户端 function,您可以使用它来动态启用/禁用验证器。 Just called it from the CheckBox's click event:刚刚从 CheckBox 的点击事件中调用它:

$(function() {
    $('#checkBoxID').click(function() {
        var validator = document.getElementById('validatorId');
        ValidatorEnable(validator, $(this).prop('checked'));
    });
});

Set the CssClass attribute on the TextBox, such as CssClass="RepClass" and try to disable the required field validator in the 'click' event:在 TextBox 上设置 CssClass 属性,例如CssClass="RepClass"并尝试在 'click' 事件中禁用必填字段验证器:

so...something like this...所以……像这样的东西……

$(".RepClass").click(function() 
{
    $(".RepClass").attr("disabled","disabled");
});

not 100% this will work, but worth a try!不是 100% 这会起作用,但值得一试!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM