使用 jquery 在中继器控件中查找嵌套的复选框列表
使用 VS 2008,我有一个带有嵌套元素的 Repeater 控件,并希望使用 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 不是当前上下文中的已知名称”。
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>
Here is my script
$('#<%= 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;
}
I get the compiler error "storeCheck is not a known name in the current context".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它不起作用,因为您尝试使用 storeCheck 的 ID 访问它,而 storeCheck 仅存在于中继器的上下文中。
你应该做的是使用类来代替。 :
因此,将代码更改为
您还可以将代码更改为此,它仅检查是否有任何带有 storeCheck 类的选中复选框:
更改了代码,因为 ASP.NET 似乎将复选框放在您提供的类的跨度内,而不是直接将其应用到复选框。
jsfiddle - http://jsfiddle.net/infernalbadger/rAVLA/1/
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:
to
You can also change you code to this which just checks if there are any checked checkboxes with the class of storeCheck:
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/