<!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'>&nbsp;1<br><input type='radio' name='nstate' value='2'>&nbsp;2<br><input type='radio'  name='nstate' value='3'>&nbsp;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>

解决方案 »

  1.   

    补充一下, 你给的代码中的一些错误:
    第一, 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'>&nbsp;1<br><input type='radio' name='nstate' value='2'>&nbsp;2<br><input type='radio'  name='nstate' value='3'>&nbsp;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>
      

  2.   

    getElementById
    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'>&nbsp;1<br><input type='radio' id='nstate2' name='nstate' value='2'>&nbsp;2<br><input type='radio' id='nstate3' name='nstate' value='3'>&nbsp;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>