简体   繁体   English

jQuery在同一行上按类名称查找包含Checkbox的单元格

[英]Jquery find cell containing Checkbox by class name on same row

Hi all and thanks in advance, 大家好,谢谢,

I've been looking at a number of the SO articles about how to find elements using Jquery but none of the variations I have concocted are working. 我一直在看许多关于如何使用Jquery查找元素的SO文章,但是我构想的所有变体都没有用。 So here's what I'm trying to accomplish, I have a gridview that has a link button that can be clicked and a check box that is wrapped in a division with a class name. 所以这就是我要完成的工作,我有一个gridview,它带有一个可以单击的链接按钮,以及一个包装在带有类名的分区中的复选框。 When the link button is clicked it calls a Jquery function like so: 单击链接按钮后,它将调用Jquery函数,如下所示:

 <asp:LinkButton ID="Remove" runat="server" Text="X" OnClientClick='RemovePerson(this,ID)' Style="color: maroon; font-weight: bold;"></asp:LinkButton>

The function itself is just: 函数本身就是:

 RemovePerson(lnk, id) {
    //find the checkbox here and uncheck it
 }

I've tried the following: 我尝试了以下方法:

var cb = $(this).closest('.tblBox').find('input[type="checkbox"]');
var cb = $(this).find('.tblBox > input[type="checkbox"]');
var cb = $(this).parent().find('td:last-child input[type="checkbox"]');

If I try to use 'lnk' instead of $(this) then the page errs out so I'm not certain if my problem is I'm passing 'this' wrong or if I'm using closest or find incorrectly or all of the above. 如果我尝试使用'lnk'而不是$(this),则该页面会出错,因此我不确定我的问题是我传递了'this'错误还是我使用了最接近或错误查找或全部以上。 What is the correct way to fix the check box on just this one row? 将复选框固定在这一行上的正确方法是什么?

UPDATE: This is the markup of the rendered gridview. 更新:这是呈现的gridview的标记。

 <table class="TableFormat fixWidth" cellspacing="0" rules="all" border="1" id="gvPOCWarden" style="border-collapse:collapse;">
   <tr>
  <th scope="col">&nbsp;</th>
      <th scope="col">Name</th>
      <th scope="col">Id</th>
      <th scope="col">Phone</th>
      <th scope="col">Email</th>
      <th scope="col">Emails</th>
      <th scope="col">Add Role</th>
    </tr>
    <tr>
   <td align="center">
      <a onclick="RemovePerson(this,&#39;RWgB/tFwYfU=&#39;);" id="lnkRemove" href="javascript:__doPostBack(&#39;ctl00$ctl00$MasterContentPlaceHolder$TabsMainContentPlaceHolder$gvPersons$ctl02$lnkRemove&#39;,&#39;&#39;)" style="color: maroon; font-weight: bold;">X</a>
    </td>
        <td>Name 1</td>
        <td>N42</td>
        <td>5686698542</td>
        <td><span id="lblEmail">name1@mail.com</span></td>
        <td align="center">                                
            <div class="pocBox">
       <input id="cbEmail" type="checkbox" name="ctl00$ctl00$MasterContentPlaceHolder$TabsMainContentPlaceHolder$gvPersons$ctl02$cbEmail" checked="checked" /><label for="cbEmail">RWgB/tFwYfU=</label>
    </div>
         </td>
         <td align="center">
    <div class="tblBox">
        <input id="cbRole" type="checkbox" name="ctl00$ctl00$MasterContentPlaceHolder$TabsMainContentPlaceHolder$gvPersons$ctl02$cbRole" checked="checked" onclick="UncheckOtherPerson(this,&#39;RWgB/tFwYfU=&#39;);" /><label for="cbRole">RWgB/tFwYfU=</label>
    </div>
          </td>
  </tr>
   </table>

I tried to setup a jsfiddle to test this but there are syntax errors that I cannot locate. 我尝试设置一个jsfiddle进行测试,但存在无法定位的语法错误。 So I took the liberty of setting up a jsfiddle that removes inline JavaScript and just take a jQuery approach - http://jsfiddle.net/jayblanchard/6mwW5/ 因此,我自由地设置了一个jsfiddle来删除内联JavaScript,而只是采用jQuery方法-http: //jsfiddle.net/jayblanchard/6mwW5/

$('table a').click(function() {
    $(this).closest('tr').find('td div input:checkbox').prop('checked', false);
});

This is generic and will work for all rows in the table. 这是通用的,适用于表中的所有行。 CAUTION: make sure that all ID's in your markup are unique, especially if you're going to use them for something. 注意:请确保标记中的所有ID都是唯一的,尤其是当您打算将其用于某些东西时。 If you decide to use them in a function you will get funky results. 如果您决定在函数中使用它们,则会得到时髦的结果。

As I said, I removed the inline JavaScript, but I added a data-id to replace the the id value that had been in the call to RemovePerson - 就像我说的,我删除了内联JavaScript,但添加了一个data-id来替换调用RemovePerson时使用的ID值-

<td align="center">
    <a data-id="&#39;RWgB/tFwYfU=&#39;" id="lnkRemove" href="#" style="color: maroon; font-weight: bold;">X</a>
</td>

I hope that this helps. 我希望这个对你有用。 Please let me know if you have any questions. 请让我知道,如果你有任何问题。

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

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