2016-02-29 75 views
0

我對Jquery來說很新,而且我正試圖找出腳本的問題。 該腳本檢查並取消選中表格中的複選框。它還會刪除TD上的類:product_select。當複選框未選中時移除TD上的課程

我的問題;標準它選擇表中的第一個複選框。該複選框被選中並且td具有類'product_select'。一切都很好,但是當我點擊這個複選框取消選中它時,TD上的類'product_select'不會被刪除。它沒有成功複選複選框,但它一直顯示類「product_select」。這發生在表格有一個記錄/ td /複選框時。

有沒有人有一個想法,通過已經選擇/選中的複選框從TD中刪除類'product_select'?

希望我清楚。提前致謝。當該複選框被選中

$(document).ready(function() { 
    $("#emp_grid td").click(function(e) { 
     $("#emp_grid td").removeClass("product_select"); 
     var $checkbox = $(this).find(':checkbox'); 
     $("#emp_grid :checkbox").not($checkbox).removeAttr("checked"); 
     if (e.target.type == "checkbox") { 
      // stop the bubbling to prevent firing the row's click event 
      e.stopPropagation(); 
      $(this).filter(':has(:checkbox)').toggleClass('product_select', $checkbox.attr('checked')); 
     } else { 
      $checkbox.attr('checked', !$checkbox.attr('checked')); 
      $(this).filter(':has(:checkbox)').toggleClass('product_select', $checkbox.attr('checked')); 
     } 
    }); 
}); 

HTML代碼:

<td class="product_select" colspan="3"> 
<input name="oplageservice" checked="checked" value="101_6" style="display:none;" type="checkbox"> € 0,01 
</td> 

HTML代碼時,我取消選中該複選框:

<td class="product_select" colspan="3"> 
<input name="oplageservice" value="101_6" style="display:none;" type="checkbox"> € 0,01 
</td> 

(類 'product_select' 不被除去)

回答

1

你能發佈表格的html嗎?

編輯:

試試這個

  $("#emp_grid td").click(function() { 
       $(this).toggleClass("product_select"); 
       if ($(this).hasClass("product_select")) { 
        $(this).find("input[type=checkbox]").attr("checked","checked"); 
       }else{ 
        $(this).find("input[type=checkbox]").removeAttr("checked"); 
       } 
      }); 
+0

克雷格嗨,該帖子的HTML代碼更新。 –

+0

試試這個:更新後的答案以顯示暗示 –

+0

太棒了,那就是訣竅。非常感謝! –

相關問題