<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript">
function ind()
{
var instate = "<form name='form1'><input type='radio' name='nstate' value='1' checked='checked'> 1<br><input type='radio' name='nstate' value='2'> 2<br><input type='radio' name='nstate' value='3'> 3</form>";
document.getElementById("mdiv").innerHTML = instate + "<a href='javascript:void(0)' onclick='s()'>safe</a>";
}
function s()
{
var arr=document.form1.nstate; //得到的是数组
for(var i=0;i<arr.length;i++)
{
if(arr[i].checked==true)
{
alert(arr[i].value);
}
}
}
</script>
</head><body>
<div id="mdiv"><a href="javascript:void(0)" onclick="ind()">dddd</a></div>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript">
function ind()
{
var instate = "<form name='form1'><input type='radio' name='nstate' value='1' checked='checked'> 1<br><input type='radio' name='nstate' value='2'> 2<br><input type='radio' name='nstate' value='3'> 3</form>";
document.getElementById("mdiv").innerHTML = instate + "<a href='javascript:void(0)' onclick='s()'>safe</a>";
}
function s()
{
var arr=document.form1.nstate; //得到的是数组
for(var i=0;i<arr.length;i++)
{
if(arr[i].checked==true)
{
alert(arr[i].value);
}
}
}
</script>
</head><body>
<div id="mdiv"><a href="javascript:void(0)" onclick="ind()">dddd</a></div>
</body>
</html>
第一, ID表示唯一标识符, 也就是它的命名唯一, 而你的代码中出现了三个id="nstate";第二,单选按钮得到的是一个数组, 需要循环判断是否选中.第三,最好把html控件加入表单中.下面给出不加入表单的代码:<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript">
function ind()
{
var instate = "<input type='radio' name='nstate' value='1' checked='checked'> 1<br><input type='radio' name='nstate' value='2'> 2<br><input type='radio' name='nstate' value='3'> 3";
document.getElementById("mdiv").innerHTML = instate + "<a href='javascript:void(0)' onclick='s()'>safe</a>";
}
function s()
{
var arr=document.all.nstate; //得到的是数组
for(var i=0;i<arr.length;i++)
{
if(arr[i].checked==true)
{
alert(arr[i].value);
}
}
}
</script>
</head><body>
<div id="mdiv"><a href="javascript:void(0)" onclick="ind()">dddd</a></div>
</body>
</html>
Returns a reference to the first object with the specified value of the ID attribute.DHTML参考手册
http://download.csdn.net/source/308913<html>
<script type="text/javascript">
function ind()
{
var instate = "<input type='radio' id='nstate1' name='nstate' value='1' checked='checked'> 1<br><input type='radio' id='nstate2' name='nstate' value='2'> 2<br><input type='radio' id='nstate3' name='nstate' value='3'> 3";
document.getElementById("mdiv").innerHTML = instate + "<a href='javascript:void(0)' onclick='s()'>safe</a>";
}
function s()
{
var cOpts = document.getElementsByName("nstate");
for (var i=0; i<cOpts.length; i++)
{
if (cOpts[i].checked)
{
alert(cOpts[i].value);
}
}
}
</script>
<body>
<div id="mdiv"><a href="javascript:void(0)" onclick="ind()">dddd</a></div>
</body>
</html>