日期:2014-05-18  浏览次数:20488 次

用JS怎么获取RadioButtonList的值?
用JS怎么获取RadioButtonList的值?
<script   language= "javascript ">
function     CheckVal()
{
var   a=document.getElementsByName( " <%=rbCemetery_BackImg.ClientID%> ");  
for(var   i=1;i <a.length;i++)
{
if(a[i].checked)  
{
alert(a[i].value);
}

}
}
</script>

我这样写了,后台
rbCemetery_BackImg.Attributes.Add( "onclick ", "javascript:CheckVal() ");

但没有反应
页面如下(为用户窗口内)
<div   style= "OVERFLOW:   auto;   HEIGHT:   222px "> <asp:radiobuttonlist   id= "rbCemetery_BackImg "   RepeatColumns= "3 "   runat= "server "   RepeatDirection= "Vertical ">
<asp:ListItem   Value= "WangMu_1.jpg "> 网墓一   <img   src= "/Images/WangMuPig/WangMu_1.jpg "   width= "125 "   height= "85 "> </asp:ListItem>
<asp:ListItem   Value= "WangMu_2.jpg "> 网墓二   <img   src= "/Images/WangMuPig/WangMu_2.jpg "   width= "125 "   height= "85 "> </asp:ListItem>
<asp:ListItem   Value= "WangMu_3.jpg "> 网墓三   <img   src= "/Images/WangMuPig/WangMu_3.jpg "   width= "125 "   height= "85 "> </asp:ListItem>
<asp:ListItem   Value= "WangMu_4.jpg "> 网墓四   <img   src= "/Images/WangMuPig/WangMu_4.jpg "   width= "125 "   height= "85 "> </asp:ListItem>
<asp:ListItem   Value= "WangMu_5.jpg "> 网墓五   <img   src= "/Images/WangMuPig/WangMu_5.jpg "   width= "125 "   height= "85 "> </asp:ListItem>
<asp:ListItem   Value= "WangMu_6.jpg "> 网墓六   <img   src= "/Images/WangMuPig/WangMu_6.jpg "   width= "125 "   height= "85 "> </asp:ListItem>
<asp:ListItem   Value= "WangMu_7.jpg "> 网墓七   <img   src= "/Images/WangMuPig/WangMu_7.jpg "   width= "125 "   height= "85 "> </asp:ListItem>
<asp:ListItem   Value= "WangMu_8.jpg "> 网墓八   <img   src= "/Images/WangMuPig/WangMu_8.jpg "   width= "125 "   height= "85 "> </asp:ListItem>
</asp:radiobuttonlist> </div>


------解决方案--------------------
document.getElementsByName( " <%=rbCemetery_BackImg.ClientID%> "); 改成
document.getElementsByName( "rbCemetery_BackImg ");
------解决方案--------------------
打错了
document.getElementsById( "rbCemetery_BackImg ");
------解决方案--------------------
你用的方法不对啊!
getElementsByName()这个是名字
getElementsById()这个才是Id
而rbCemetery_BackImg.ClientID是获得id
------解决方案--------------------
var a=document.getElementsByTagName( "input ");
for(var i=0;i <a.lenght;i++)
{
if(a[i].type== "radio " && a[i].id.indexOf( "rbCemetery_BackImg ")> 0)
{
alert(a[i].value);
}
}
------解决方案--------------------