如何获得中继器中复选框的唯一ID? [英] How do I get the unique id for checkboxes in a repeater?

查看:95
本文介绍了如何获得中继器中复选框的唯一ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的中继器在用户控件中,并使用asp:repeater.这是我的代码:

My repeater is in a user control and uses an asp:repeater. Here is my code:

 <asp:Repeater ID="rptrNotOnFile" Runat="server">
        <ItemTemplate>      
            <tr>
               <td>&nbsp;</td>
               <td>Item <asp:Label ID="NotOnFileItemNumber" Runat="server" />:
                   <asp:Label ID="lblPartNumberNotOnFile" Runat="server" />
               </td>
               <td>
                  <asp:Checkbox id="chkPartSelected" runat="server" onClick="isSamChecked()"/>
               </td>
               <td>
                   <asp:Checkbox id="chkPartWatchParts" runat="server"/>
               </td>               
               <td>
                   <asp:Checkbox id="samSelected" runat="server" onClick="isMbChecked()"/>
                                   </td>               
            </tr>   
        </ItemTemplate>        
    </asp:Repeater>

每个转发器项中都有chkPartSelected和samSelected复选框,我试图取消选中其中一个,反之亦然.

The checkboxes chkPartSelected and samSelected are in each repeater item and I am trying to uncheck one when the other is checked and vice versa.

这是我的jquery函数:

Here's my jquery functions:

<script>
 function isMbChecked() {


         if ($('#ResultsNotOnFile_rptrNotOnFile_ctl01_chkPartSelected').is(":checked"))

             ($('#ResultsNotOnFile_rptrNotOnFile_ctl01_chkPartSelected').prop("checked", false))


     }
     function isSamChecked() {

               if ($('#ResultsNotOnFile_rptrNotOnFile_ctl01_samSelected').is(":checked"))

                   ($('#ResultsNotOnFile_rptrNotOnFile_ctl01_samSelected').prop("checked", false))

     }
</script>

当我对中继器中每个复选框的唯一ID进行硬编码时,这些函数起作用,但是我不希望对其进行硬编码,因为我可以拥有一个或多个中继器项目.

These function work when I hard code the unique id for each checkbox in the repeater, but I do not want it hardcoded because I can have one or many repeater items.

如何获取唯一的ID并将其传递给函数?

How do I get that unique id and pass it to the functions?

感谢您的帮助

JGraham

推荐答案

如果我是你,我会放弃ID,因为它们不是唯一的,并在其中添加一些类以使其更易于识别.

If I were you, I would just ditched the IDs since they aren't unique, and add some classes to these to make them more identifiable.

例如

<asp:Checkbox runat="server" class="sam" onClick="isSamChecked()"/>
<asp:Checkbox runat="server" class="mb" onClick="isMbChecked()"/>

然后您可以做类似的事情

Then you could do something like

<script>
 function isMbChecked() {
   var closest = $(this).parent('td').siblings().children('.sam');
   closest.prop('checked', !closest.prop('checked'));
 }

 function isSamChecked() {
   var closest = $(this).parent('td').siblings().children('.mb');
   closest.prop('checked', !closest.prop('checked'));
 }
</script>

这篇关于如何获得中继器中复选框的唯一ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆