在带有jQuery的转发器控件中找到嵌套的复选框列表 [英] find nested checkboxlist in repeater control with jquery
问题描述
使用VS 2008,我有一个带有嵌套元素的Repeater控件,并希望使用jquery选择其中一个(复选框).
Using VS 2008, I have a Repeater control with nested elements and want to select one of them (the checkboxes) with jquery.
<asp:Repeater runat="server" ID="storesRep" DataSourceID="storeSqlDataSource" OnItemDataBound="StoresRep_ItemDataBound">
<ItemTemplate>
<table style="padding:0px">
<tr>
<td style="width:200px"><asp:Label ID="infoLbl" runat="server">Choose stores for upload:</asp:Label> </td>
<td style="width:110px"><asp:Label ID="storeLbl" runat="server" Text='<%# Bind("Name") %>'></asp:Label> </td>
<td><asp:CheckBox runat="server" ID="storeCheck" CssClass="storeCheck" /></td>
</tr>
</table>
</ItemTemplate>
<FooterTemplate>
<table runat="server" id="footerTbl" visible="false" style="padding:0px">
<tr>
<td style="width:200px"><asp:Label ID="infoLbl" runat="server">Choose stores for upload:</asp:Label> </td>
<td><asp:Label ID="lblEmptyData" Text="No Stores found." runat="server" ForeColor="GrayText"></asp:Label></td>
</tr>
</table>
</FooterTemplate>
</asp:Repeater>
这是我的剧本
$('#<%= uploadBtn.ClientID %>').click(
function() {
//check if store was chosen from list
var storeChecked = false;
$('#<%= storeCheck.ClientID %>').each(function() {
if ($(this).attr('checked'))
storeChecked = true;
});
if (storeChecked == false) {
alert("Upload is only possible if a store has been chosen from list.");
return false;
}
我收到编译器错误"storeCheck在当前上下文中不是已知名称".
I get the compiler error "storeCheck is not a known name in the current context".
推荐答案
它不起作用,因为您尝试使用其ID访问storeCheck,而storeCheck仅存在于转发器的上下文中.
It's not working because you are trying to access storeCheck using it's ID and storeCheck only exists in the context of the repeater.
您应该做的是使用该类.因此更改:
What you should do is use the class instead. So change:
$('#<%= storeCheck.ClientID %>').each(function() {
到
$('.storeCheckBox').each(function() {
您还可以将代码更改为此,仅检查是否有带有storeCheck类的已选中复选框:
You can also change you code to this which just checks if there are any checked checkboxes with the class of storeCheck:
$('#<%= uploadBtn.ClientID %>').click(function() {
if($('span.storeCheck input:checked').length == 0) {
alert("Upload is only possible if a store has been chosen from list.");
return false;
}
});
更改了代码,因为asp.net将复选框与您提供的类放在一个跨度内,而不是直接将其应用于该复选框.
Changed the code as it appears that asp.net puts the checkbox inside a span with the class you supply rather than applying it straight to the checkbox.
jsfiddle- http://jsfiddle.net/infernalbadger/rAVLA/1/
jsfiddle - http://jsfiddle.net/infernalbadger/rAVLA/1/
这篇关于在带有jQuery的转发器控件中找到嵌套的复选框列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!