2010-06-20 105 views
1

當用戶按下按鈕時,需要更改包含單選按鈕的div的bg顏色。我正在使用jquery - 所以它似乎應該有一些簡單的方法來實現這一點。我有我的HTML設置是這樣的:選擇收音機btn時更改bg顏色

<div class="sales_box"> 
<table> 
<tr> 
<td class="radio_cell"><input type="radio"></td> 
</tr> 
</table> 
</div> 

<div class="sales_box"> 
<table> 
<tr> 
<td class="radio_cell"><input type="radio"></td> 
</tr> 
</table> 
</div> 

回答

6

剛剛處理的單選按鈕的變化事件,然後使用jQuery的closest()方法:

$(document).ready(function(){ 
    $('input:radio').change(function(){ 
     $(this).closest('div').css('background-color', 'red'); 
    }); 
}); 

而且作爲redsquare說,你不應該」真的設置內聯的CSS。您應該使用toggleclass函數。我只是用css函數作爲例子。

+0

這將改變TD背景 – redsquare 2010-06-20 09:20:26

+0

@redsquare - 無需downvote,給我改變,先改正它。 – GenericTypeTea 2010-06-20 09:22:06

+0

我沒有-1它 – redsquare 2010-06-20 09:22:30

0

嘗試

$(function() { 
     $(".radio_cell input[type=radio]").bind("click", function() { 
      $(this).parents('div:first').css("background-color", "Blue"); 
     }); 
    }); 
+1

最接近在這裏,這種方式得到所有的父母然後過濾器 – redsquare 2010-06-20 09:27:40

0

我會用toggleClass而不是設置內聯CSS。

$(document).ready(function(){ 
    $('input:radio').change(function(){ 
     $(this).closest('div').toggleClass('highlight'); 
    }); 
}); 
+0

我剛剛添加了一個關於取消選擇行爲的後續行動: http://stackoverflow.com/questions/3078781/change-bg-on-radio-deselect – Thomas 2010-06-20 09:43:29