请问如何给一个radioButtonList里的每个radioButton添加javascript事件?
我是用asp.net做的页面,里面有个RadioButtonList,我该怎么使用JQuery,才能让每个radiobutton都加载上一个click事件,
我是想让用户在点击了某个radio之后,该radio变成一种颜色,其他所有没有被点击的都是另一个颜色。
大家可以看到最后的代码, 我有两个radioButtonList,其中有一个是在updatePanel里面,做ajax异步请求,其中的内容会因为第一个radioButtonList的index变化而变化。
我使用的javascript如下:
$(document).ready(
function() {
$(':radio').click(
function() {
$(':radio').siblings().css('color', 'red');
$(this).siblings().css('color', 'blue');
}
);
}
);
我的想法是,找到所有的radio,然后给radio选中的字变成蓝色,该组其他button(即没有被选中的)变红。
但是有两个问题:
1) 因为是异步传送,所以在updatePanel里的radio没办法加载javascript
2) 如果我把radiobutton从updatePanel中取出的话,改变任意一组radio的index,另一组被选中的radio也会跟着变成红色。。。
我又希望能够使用ajax,又希望各组radiobuttonlist互不干扰,该怎么写呢???
以下是生成的html:
<table id="ctl00_ContentPlaceHolder1_RadioButtonList_CategoryList" class="RadioButtonList" border="0">
<tr>
<td><input id="ctl00_ContentPlaceHolder1_RadioButtonList_CategoryList_0" type="radio" name="ctl00$ContentPlaceHolder1$RadioButtonList_CategoryList" value="1" onclick="javascript:setTimeout('__doPostBack(\'ctl00$ContentPlaceHolder1$RadioButtonList_CategoryList$0\',\'\')', 0)" /><label for="ctl00_ContentPlaceHolder1_RadioButtonList_CategoryList_0">购物</label></td><td><input id="ctl00_ContentPlaceHolder1_RadioButtonList_CategoryList_1" type="radio" name="ctl00$ContentPlaceHolder1$RadioButtonList_CategoryList" value="2" onclick="javascript:setTimeout('__doPostBack(\'ctl00$ContentPlaceHolder1$RadioButtonList_CategoryList$1\',\'\')', 0)" /><label for="ctl00_ContentPlaceHolder1_RadioButtonList_CategoryList_1">饮食</label></td>
</tr>
</table>
<div id="ctl00_ContentPlaceHolder1_UpdatePanel1">
<table id="ctl00_ContentPlaceHolder1_RadioButtonList_TagList" class="RadioButtonList" border="0">
<tr>
<td><input id="ctl00_ContentPlaceHolder1_RadioButtonList_TagList_0" type="radio" name="ctl00$ContentPlaceHolder1$RadioButtonList_TagList" value="2d8d3176-d395-4ccc-b2cc-314983a7dc3e" /><label for="ctl00_ContentPlaceHolder1_RadioButtonList_TagList_0">超市</label></td><td><input id="ctl00_ContentPlaceHolder1_RadioButtonList_TagList_1" type="radio" name="ctl00$ContentPlaceHolder1$RadioButtonList_TagList" value="f906a264-7158-4138-9c06-ce6871ad6831" /><label for="ctl00_ContentPlaceHolder1_RadioButtonList_TagList_1">男装</label></td>
</tr>
</table>
</div>
谢谢!!