<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="practise.WebForm1" %>
<!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>
<script type="text/javascript" language="javascript">
function validate() {
var rb_sex = document.getElementById("rb_sex");
var sex = rb_sex.getElementsByTagName("INPUT");
/*
此方法中,RadioButtonList在客户端被看成成了table,通过getElementsByTagName("INPUT")方法获取它的所有子radio,
然后循环每个radio,再通过cells获取radio的text值。
*/
var result = false;
for (var i = 0; i < sex.length; i++) {
if (sex[i].checked) {
result = true;
var text = rb_sex.cells[i].innerText;
var value = sex[i].value;
alert("选中项的text值为" + text + ",value值为" + value);
break;
}
}
if (!result) {
alert("请选择性别!");
return false;
}
return true;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:RadioButtonList ID="rb_sex" runat="server">
<asp:ListItem Value="1">男</asp:ListItem>
<asp:ListItem Value="2">女</asp:ListItem>
</asp:RadioButtonList>
<input type="button" id="t_validate" runat="server" value="验证" οnclick="validate();" />
</div>
</form>
</body>
</html>