当用jquery单击表行时,选中复选框addclass

时间:2015-02-18 09:43:17

标签: javascript jquery

我有一张桌子,我想查看复选框&当用户点击表格行时,为div添加一个类,我无法添加该类来添加该类。 任何人都可以告诉我哪里出错了。

HTML BELOW:

<div class="widgit">
<table id="QR_table">
 <thead>
   <tr class="QR_table-row">
     <th class="qr_action">Checkbox</th>
     <th class="qr_action">Action</th>
     <th class="qr_from">FROM</th>
      <th class="qr_to">TO</th>
      <th class="qr_trans">Transport</th>
    </tr>
</thead>
<tbody>
    <tr class="QR-row">
        <td class="checkbox">
            <input class="modal-state" id="modal-1" type="checkbox" />
        </td>
        <td class="qr_action">PT</td>
        <td class="qr_from">4</td>
        <td class="qr_to">21</td>
        <td class="qr_trans">WC</td>
    </tr>
    <tr class="QR-row">
        <td class="checkbox">
            <input class="modal-state" id="modal-1" type="checkbox" />
        </td>
        <td class="qr_action">PT</td>
        <td class="qr_from">8</td>
        <td class="qr_to">2</td>
        <td class="qr_trans">T</td>
    </tr>        
</tbody>

JQUERY BLOW:

$(function() {
  $(".QR-row").click(function() {
    var $chk = $(this).find('input[type=checkbox]');
    $chk.prop('checked',!$chk.prop('checked'));
       if($($chk).is(':checked')){
        $(".widgit").addClass("modal-open");  
        } else {
          $(".widgit").removeClass("modal-open");
        }   
  });               
});

JSFIDDLE

1 个答案:

答案 0 :(得分:0)

从我的观点来看,一切正常。

在你的小提琴中,你所拥有的唯一一个div就在你的桌子周围,它似乎是你试图给一个班级的那个。

<div class="widgit">
<table id="QR_table">

一切正常。 Fiddle

如果你想要它做其他事情,那么你必须更加具体地了解你想要实现的目标

相关问题