2010-06-07 131 views
1
$("tr.clickable").each(function() {$(this).click(function() { 
      $(this).children("td:first > input").is(":checked") ? 
        $(this).children("td:first > input").removeAttr("checked") : 
        $(this).children("td:first > input").attr("checked","checked"); 
     })}); 

这只会检查表格中的第一行(不管我点击哪一行)。我如何应用它以检查我点击的特定行上的输入?通过孩子操纵检查表格中的复选框

HTML:

<tbody style="height: 500px; overflow-x: hidden; overflow-y: scroll;"> 
               <tr id="row0" class="alternate clickable"> 
         <td>       <img width="11" height="11" title="This email failed to send." alt="Failed" src="images/error_warning.png"> 
                <br> 
          <input type="checkbox" value="6751" name="emailCheck[]" class="emailCheck"></td> 
         <td><a href="user_settings.php?id=349"> 
          wmiller     </a></td> 
         <td>New Fin-iQ Message</td> 
         <td><span class="errorText">UNKNOWN</span></td> 
         <td>New Bulletin Notification</td> 
         <td>Jun. 07/10 10:14:39 am</td> 
         <td><a href="emails_report.php?action=re&amp;id=6751">Resend</a> 
          <br> 
          <a id="emailBodyToggle_6751" href="javascript:showEmail(6751)">Show</a> 
         </td> 
        </tr> 
<tr id="emailBody_6751" style="display: none; text-align: left;" class="alternate"> 
        <td colspan="7"><div>...</div></td> 
       </tr> 
               <tr id="row1" class=" clickable"> 
        <td>       <img width="11" height="11" title="This email failed to send." alt="Failed" src="images/error_warning.png"> 
               <br> 
         <input type="checkbox" value="6752" name="emailCheck[]" class="emailCheck"></td> 
        <td><a href="user_settings.php?id=350"> 
         mholman     </a></td> 
        <td>New Fin-iQ Message</td> 
        <td><span class="errorText">UNKNOWN</span></td> 
        <td>New Bulletin Notification</td> 
        <td>Jun. 07/10 10:14:39 am</td> 
        <td><a href="emails_report.php?action=re&amp;id=6752">Resend</a> 
         <br> 
         <a id="emailBodyToggle_6752" href="javascript:showEmail(6752)">Show</a> 
        </td> 
       </tr> 

       <tr id="emailBody_6752" style="display: none; text-align: left;" class=""> 
        <td colspan="7"><div>...</div></td> 
       </tr> 
+0

只是为了验证,所有tr(#row1,#row2等我假设)也有类'可点击'? – 2010-06-07 17:39:48

+0

是的,所有#行%ID%s都有'可点击'。我只是在最后扩展html来说明=) – gsquare567 2010-06-07 18:15:04

回答

4

尝试删除调用。每个()在您的第一选择。 jQuery使用“隐含迭代”,这意味着你可以做这样的事情:

$('tr.clickable').click(function() { // Do things here. }); 

你应该用这种方式在每一行点击功能的结束。

你的文章上摆弄搞乱后,我得到这个工作:

$("tr.clickable").click(function() { 
    $(this).find("td:first input").is(":checked") ? $(this).find("td:first input").removeAttr("checked") : $(this).find("td:first input").attr("checked","checked"); 
}); 
+0

伟大的观点..知道有什么东西没有看起来'正确'的代码,但不能把我的手指放在它上面。 +1 – 2010-06-07 17:57:14

+0

我之前试过:这就是为什么我把它放在它周围,因为它看起来像只是触及第一个元素 – gsquare567 2010-06-07 18:03:06

+0

这是一个单独的范围问题,不能解决内容问题:http://jsfiddle.net/mH7NE /虽然这是正确的,但它不能解决问题:) – 2010-06-07 18:48:21

0

您可以切换左右你的代码是这样的:

$("tr.clickable").click(function() { 
    var cb = $("td:first > input", this)[0]; 
    cb.checked = !cb.checked; 
});​ 

You can view a demo on it here

使用核心JavaScript方法和.checked DOM attribute,这是更清洁/更快,仍然跨浏览器兼容,只使用jQuery的选择器。

网站注:当绑定像.click()的处理程序没有必要对.each(),只需拨打.click()直接和功能/句柄传递将被绑定到每一个匹配元素。

+0

我有很高的期望,但仍然是一个nogo。如果有帮助,编辑后的主文章更多html = S – gsquare567 2010-06-07 18:10:41

+0

@ gsquare567:用新的标记更新了演示,给它一个:) – 2010-06-07 18:20:57