2011-11-15 75 views
0

我試圖使用JQuery更改以下標籤的css,但我無法弄清楚。這實際上是我的checkboxlist(asp.net)中的html,這是迄今爲止我所擁有的。任何人都可以幫忙嗎?謝謝。更改已選中複選框的標籤css

下面的JQuery查找整個表格,並使所有標籤變成紅色,而不僅僅是我檢查的標籤。

$("#CheckBoxList1").click(function() { 
    $(this).find('label').removeClass('red'); 
    if ($('span').hasClass('checked')) 
     $(this).find('label').addClass('red'); 
}); 

<table id="CheckBoxList1" border="0"> 
    <tbody> 
    <tr> 
     <td> 
     <div class="checker" id="uniform-CheckBoxList1_0"> 
      <span> 
      <input id="CheckBoxList1_0" type="checkbox" name="CheckBoxList1$0" style="opacity: 0; "/> 
      </span> 
     </div> 
     <label for="CheckBoxList1_0">Mark Park</label> 
     </td> 
     <td> 
     <div class="checker" id="uniform-CheckBoxList1_1"> 
      <span> 
      <input id="CheckBoxList1_1" type="checkbox" name="CheckBoxList1$1" style="opacity: 0; "/> 
      </span> 
     </div> 
     <label for="CheckBoxList1_1">Amy Lee</label> 
     </td> 
     <td/> 
    </tr> 
    </tbody> 
</table> 

回答

1
$("#CheckBoxList1").click(function() { 
    $(this).find('label').removeClass('red'); 
    $('span.checked').parent().next('label').addClass('red'); 
}); 
+0

這是做到了。謝謝。 – MikeB55

0

你能一類/ ID添加到標籤和複選框,並訪問它的方式:

$(".check").click(function() { 
    var id = $(this).attr("id").split("_"); 
    //access array of id 
    $(".label_"+id[1]).addClass("red"); 
} 

然後添加ID /班到您的html:

<input id="CheckBoxList1_0" id="check_1" type="checkbox" class="check" name="CheckBoxList1$0" style="opacity: 0; "/> 

<label for="CheckBoxList1_0" id="label_1">Mark Park</label> 
+0

我試過這個,但沒有奏效。 – MikeB55

+0

對不起,複選框的事件是「更改」而不是「點擊」。那麼您可能需要檢測該點是否已選中或未選中。 –

0

嘗試類似這樣:

$("label[for='comedyclubs']").toggleClass("red");