后台
protected void Page_Load(object sender, EventArgs e)
{
for (int i = 0; i < RadioButtonList1.Items.Count; i++)
{
RadioButtonList1.Items[i].Attributes.Add("onclick", "javascript:SelectCheck();");
}}
前台<asp:RadioButtonList ID="RadioButtonList1" Name="RadioButtonList1" runat="server">
<asp:ListItem Text="1" Value="1"></asp:ListItem>
<asp:ListItem Text="2" Value="2"></asp:ListItem>
<asp:ListItem Text="3" Value="3"></asp:ListItem></asp:RadioButtonList>function SelectCheck()
{
var rbl=document.getElementById('<%= RadioButtonList1.ClientID %>'); for(i = 0; i < rbl.childNodes.length; i ++)
{
if (rbl.childNodes[i].tagName == "INPUT"&&rbl.childNodes[i].checked)
{
var ItemSelected=rbl.childNodes[i].value;
alert(ItemSelected);
}
else
{
alert("找不到选中的项");
}
}
}另外用document.getElementsByName+(源文件里的RadioButtonList的name)也试验过,同样不行ItemSelected里的一直是undefined,而不是我想要得到的 RadioButtonList中选中的value值。JS写成function SelectCheck()
{
var rbl=document.getElementById('<%= RadioButtonList1.ClientID %>'); for(i = 0; i < rbl.length; i ++)
{
if (rbl[i].tagName == "INPUT"&&rbl[i].checked)
{
var ItemSelected=rbl[i].value;
alert(ItemSelected);
}
else
{
alert("找不到选中的项");
}
}
}也不行。
求助
function check()
{
var obj=document.getElementsByName("RadioButtonList1");
for(i = 1; i < obj.length; i ++)
{
alert(obj[i].checked);
}
}
</script>这样可以用啊。 。要用getElementsByName就好了。。
childNodes取出来的应该是tr行吧,
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="WebApplication1.WebForm2" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:RadioButtonList runat="server" ID="myRadio">
<asp:ListItem Selected="true" Text="a" Value="a"></asp:ListItem>
<asp:ListItem Selected="False" Text="b" Value="b"></asp:ListItem>
<asp:ListItem Selected="False" Text="c" Value="c"></asp:ListItem>
<asp:ListItem Selected="False" Text="d" Value="d"></asp:ListItem>
</asp:RadioButtonList>
</div>
</form>
</body>
<script type="text/javascript">
function test()
{
//方案1
var radioList = document.getElementsByName("<%=myRadio.ClientID %>");
for(var i = 0;i <radioList.length;i++)
{
if(radioList[i].checked)
{
alert(radioList[i].value);
}
}
}
test(); function test1() {
//方案二
var inputList = document.getElementsByTagName("input");
for (var i = 0; i < inputList.length; i++) {
var item = inputList[i];
if (item.type.toLowerCase() == "radio" && item.checked) {
alert(item.value);
}
}
}
test1();
</script>
</html>
下拉菜单怎么调 onclick 方法 ? 明显是 onChange !
zhubosa朋友的方案二可以实现。
奇怪的是用getElementsByName("<%=myRadio.ClientID %>");就是不行。。