JS取得RadioButtonList的Value,Text及选中值等信息

Code
<asp:RadioButtonList ID="RadioButtonList1" runat="server"
RepeatColumns
="4" RepeatDirection="horizontal">
    
<asp:ListItem Value="1" Text="A1"></asp:ListItem>
    
<asp:ListItem Value="2" Text="A2"></asp:ListItem>
    
<asp:ListItem Value="3" Text="A3"></asp:ListItem>
    
<asp:ListItem Value="4" Text="A4"></asp:ListItem>
    
<asp:ListItem Value="5" Text="A5"></asp:ListItem>
    
<asp:ListItem Value="6" Text="A6"></asp:ListItem>
    
<asp:ListItem Value="7" Text="A7"></asp:ListItem>
    
<asp:ListItem Value="8" Text="A8"></asp:ListItem>
    
<asp:ListItem Value="9" Text="A9"></asp:ListItem>
    
<asp:ListItem Value="10" Text="A10"></asp:ListItem>
</asp:RadioButtonList>

<input id="Button1" type="button" value="button" onclick="fn_GetRadioButtonListInfo();" />

 

Code
function fn_GetRadioButtonListInfo()
{
    
//取得RadioButtonList的集合
    var radListItems = document.all("RadioButtonList1");
    
if(radListItems==null)
    
{
         alert(
"相关对象对空");
        
return false;
     }

    
//弹出RadioButtonList的Item的个数
    var radListItesCount = radListItems.length - 1 ;
     alert(
"Item个数"+radListItesCount);
    
    
var radListCheckedValue = "";
    
//遍历Item的Text和Value
    for(var i = 1; i <=   radListItesCount ; i++ )
    
{
        
var itemInfo = "";
         itemInfo
+= "" + i +"Item ";
        
//Value
         itemInfo += " Value: "+ radListItems[i].value;
        
//Text
        //itemInfo += " Text: "+ radListItems[i].nextSibling.innerText ;
        //或者
         itemInfo += " Text: "+ radListItems[i].parentElement.childNodes[1].innerText ;
        
//是否是选中
         itemInfo += " 是否选中: "+ radListItems[i].checked;
        
//
        if(radListItems[i].checked)
             radListCheckedValue
= radListItems[i].value;
         alert(itemInfo);
     }

    
if(radListCheckedValue=="")
    
{
         alert(
"没有选中值");
     }

    
else
    
{
         alert(
"选中Value 为 : "+radListCheckedValue);
     }

    
return false;
}

 

<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="ajaxselect.OnmouseTitle.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
    <HEAD>
        <title>WebForm1</title>
        <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
        <meta content="C#" name="CODE_LANGUAGE">
        <meta content="JavaScript" name="vs_defaultClientScript">
        <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
        <script language="javascript">
       
               function getvalue()
        {
        var b=document.all.rbtid.length
        var a=document.getElementById("rbtid").cells.length;
        //alert(b);结果为5
        //alert(a);结果为4
        //for(var i=0;i<b-1;i++)这样也行
        for(var i=0;i<a;i++)
        {
          var ss="rbtid_"+i;
          var aa=document.getElementById(ss).value;
          //if(eval('document.all.rbtid_'+i).checked==true) //这样也行 eval()函数能将数据符串转成js运行
          var bb=document.getElementById(ss);
          if(document.getElementById(ss).checked) //注意checked不能写成Checked,要不然不成功
           {
                  alert(aa);
                  break;
           }     
       
        }
        }
        </script>
    </HEAD>
    <body MS_POSITIONING="GridLayout">
        <form id="Form1" method="post" runat="server">
            <FONT face="宋体">
                <asp:radiobuttonlist id="rbtid" style="Z-INDEX: 101; LEFT: 8px; POSITION: absolute; TOP: 8px" runat="server"
                    Width="216px" Height="176px" name="rbtid">
                    <asp:ListItem Value="0">0</asp:ListItem>
                    <asp:ListItem Value="1" Selected>1</asp:ListItem>
                    <asp:ListItem Value="2">2</asp:ListItem>
                    <asp:ListItem Value="3">3</asp:ListItem>
                </asp:radiobuttonlist><INPUT style="Z-INDEX: 102; LEFT: 184px; POSITION: absolute; TOP: 256px" onclick="getvalue()"
                    type="button" value="Button"></FONT>
        </form>
    </body>
</HTML>

你可能感兴趣的:(RadioButton)