简体   繁体   English

通过JavaScript重复中继器的复选框时出错

[英]error when iterating repeaters' checkbox through javascript

HTML HTML

 <script type="text/javascript" language="javascript">
    function toggleSelectionRepeater(source) {        
        var isChecked = source.checked;            
        $("#tblListing input[id*='chkListingName']").each(function(index) {
            alert('in');
            $(this).attr('checked', false);
        });
        source.checked = isChecked;
    }

 </script>  

<div id="tblListing" runat="server">
<table>
<asp:Repeater ID="rptrPictureList" runat="server" OnItemDataBound="rptrPictureList_ItemDataBound">
<ItemTemplate>        
<tr>
<td><asp:CheckBox ID="chkListingName" runat="server" CssClass="cb"  /></td>        
<td></td>
</tr>
</ItemTemplate>
</asp:Repeater>
<tr><td><div><input type="submit"  name="cmdSendEmail" id="cmdSendEmail" value="Send Email/s"  onclick="toggleSelectionRepeater(this);" runat="server" /></div></td></tr>     
</table>
</div>

I want to check on button click if any of the checkbox in repeater is selected or not. 我想检查是否单击了Repeater中的任何复选框的按钮单击。 If any checkbox is selected then only I need to send email so I want to loop through the repeater. 如果选中了任何复选框,则仅我需要发送电子邮件,因此我想遍历转发器。 With this code I am getting javascript error "Object expected'. How do I loop through the repeater control. What should i change to make it run? 通过此代码,我得到了javascript错误“对象预期”。如何在转发器控件中循环。我应该进行哪些更改以使其运行?

The asp:CheckBox is different on the client side as ASP.NET controls rendered to html have another id there. asp:CheckBox在客户端方面有所不同,因为呈现为html的ASP.NET控件在此具有另一个ID。 If you want to work with the ID you should add the ClientIDMode="Static" attribute to the checkbox. 如果要使用ID,则应将ClientIDMode="Static"属性添加到复选框。 But you should not use ID as you repeat them, hence you have multiple times the same ID. 但是,重复使用它们时,请勿使用ID,因此,同一ID会多次出现。 Here is a workaround JSFiddle 这是JSFiddle解决方法

Replace the id with the class or use another one if you need the ID for asp.net. 将ID替换为该类,或者如果需要asp.net的ID,请使用另一个ID。

<asp:CheckBox class="chkListingName" runat="server" CssClass="cb"/>

But in your JS use the class. 但是在您的JS中使用该类。

$(".chkListingName").each(function(index) {
    $(this).attr('checked', false);
});

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

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