如何在asp:CustomValidator javascript函数中获取父级,以便我可以用它来检查是否已经检查了相关的复选框?
示例:
我有以下代码:
<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") %>' />
<asp:CustomValidator id="CustomValidator2" runat="server"
ControlToValidate = "repID"
ErrorMessage = ""
ClientValidationFunction="test" >
</asp:CustomValidator>
</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>
Jquery代码:
function test(oSrc, args) {
var $tb = $('input[id$=employee_name]');
// only validate if an <asp:Checkbox> called chkOtherCheckbox is ticked
alert(args.Value.toString())
if ($('input:checkbox[id$=chkOtherCheckbox]').is(':checked'))
alert('checked')
else
alert('no checked')
}
如何在测试功能中获取父级,以便我可以用它来检查是否已选中相关复选框?
答案 0 :(得分:0)
您可以使用此代码获取最近的复选框状态。
function test(oSrc, args) {
var nearestCheckBox= $(oSrc).parent().find('input:checkbox:first');
var state=nearestCheckBox.attr('checked');//use this variable
}