var a=document.getElementsByName("box");
for(var i=0;i<a.length;i++){
    if(a[i].checked==true){
       document.getElementsByName("txt")[i].value="ok";
      
    }
}
还可以通过表直接控制

解决方案 »

  1.   

    非常感谢,可是这个必须通过循环才能得到box的索引号吗?有没有方法直接获得触发事件的对象的的索引号?
      

  2.   

    给你定做了个例程,你看看:
    <%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <title>无标题文档</title>
    <script language="javascript">
    function setFlag(aObj)
    {
    if (!aObj)
    return false;
    for (i=0;i<aObj.form.box.length;i++)
    if (aObj.form.box[i]==aObj)
    if (aObj.form.box[i].checked)
    aObj.form.txt[i].value="ok";
    else
    aObj.form.txt[i].value="";
    }
    </script>
    </head><body>
    <form name="form1" method="post" action="">
    1<input name="box" type="checkbox" id="box" value=""  onClick="javascript:setFlag(this);"/><input name="txt" type="text" id="txt" /><br>
    2<input name="box" type="checkbox" id="box" value=""  onClick="javascript:setFlag(this);" /><input name="txt" type="text" id="txt" /><br>
    3<input name="box" type="checkbox" id="box" value=""  onClick="javascript:setFlag(this);" /><input name="txt" type="text" id="txt" />
    </form>
    </body>
    </html>
    -------------------
    http://kmok.cn/