请问如何给一个radioButtonList里的每个radioButton添加javascript事件?

wappibz 2009-03-14 04:13:31
我是用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>

谢谢!!
...全文
580 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
wappibz 2009-03-14
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 liuyeede 的回复:]
既然要使用radioButtonList又想完全在客户端控制,那你还要Asp.net干什么?
[/Quote]

我是想看看有没有别的办法加载异步回送的radioButtonList

asp.net在这里是用来加载数据的,如果可以在客户端加载js,那么可以节省服务器端的资源,虽然只是一点点,但是多了就有用了。 当然,做不到的话还是只有asp.net来做。
endyli 2009-03-14
  • 打赏
  • 举报
回复
protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
{
foreach (ListItem i in this.RadioButtonList1.Items)
{
if(i.Selected)
i.Attributes.Add("style","color=red");
else
i.Attributes.Add("style", "color=blue");
}
}
liuyeede 2009-03-14
  • 打赏
  • 举报
回复

你也可也吧Javascript代码在客户端写好,在服务端注册,然后this.RadioButtonList1.Items[i].Attributes.Add("onclick", "javascript函数名()")
pgameli 2009-03-14
  • 打赏
  • 举报
回复
在源文件里面把js函数写好
然后在页面cs文件里面添加就行了
for(int i=0;i<RadioButtonList1.Items.Count;i++)
{
this.RadioButtonList1.Items[i].Attributes.Add("onclick", "js函数名()");
}
liuyeede 2009-03-14
  • 打赏
  • 举报
回复
既然要使用radioButtonList又想完全在客户端控制,那你还要Asp.net干什么?
wappibz 2009-03-14
  • 打赏
  • 举报
回复
可不可以在客户端做这个事啊?
pgameli 2009-03-14
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 liuyeede 的回复:]
C# codefor(int i=0;i<RadioButtonList1.Items.Count;i++)
{
this.RadioButtonList1.Items[0].Attributes.Add("onclick", "javascript:....");
}
[/Quote]
修改一下

for(int i=0;i<RadioButtonList1.Items.Count;i++)
{
this.RadioButtonList1.Items[i].Attributes.Add("onclick", "javascript:....");//这里应该是i
}
liuyeede 2009-03-14
  • 打赏
  • 举报
回复
for(int i=0;i<RadioButtonList1.Items.Count;i++)
{
this.RadioButtonList1.Items[0].Attributes.Add("onclick", "javascript:....");
}

62,267

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧