<strong>一. 测试环境</strong> <br>浏览器: IE6+, FF 3.5.5, Opera 10, Chrome 4.0.249, Safari 4.0.3 <br><br><strong>二. 例子 <br></strong><div class="codetitle"> <span><a style="CURSOR: pointer" data="7225" class="copybut" id="copybut7225" onclick="doCopy('code7225')"><u>复制代码</u></a></span> 代码如下:</div> <div class="codebody" id="code7225"> <br><form name="test-form" action="" method=""> <br><input type="checkbox" name="kk"> <br> </form> <form> <br><script type="text/javascript"> <BR>var oForm = document.forms['test-form'], <BR>eles = oForm.elements['kk']; <BR>alert(eles.length); // undefined <BR>alert(eles.nodeType); // 1 <BR></script> <br> </form> </div> <br><strong>三. 解决方法(我想到的方法是改变获取方式, 基于YUI)</strong> <br><div class="codetitle"> <span><a style="CURSOR: pointer" data="85199" class="copybut" id="copybut85199" onclick="doCopy('code85199')"><u>复制代码</u></a></span> 代码如下:</div> <div class="codebody" id="code85199"> <br><script type="text/javascript"> <BR>var oForm = document.forms['test-form'], <BR>eles = YAHOO.util.Dom.getElementsBy(function(el) { <BR>return el.type === 'checkbox' && el.name === 'kk'; <BR>}, 'input', oForm); <BR>alert(eles.length); // 1 <BR></script> <br> </div> <br><br><br>document.formname.inputname <br><br>这个问题我之前已经遇到过一次了,但是这次又忘记了,依然又犯错了,所以我必须要记录一下。 <br><br>看一下这个例子: <br><div class="codetitle"> <span><a style="CURSOR: pointer" data="24720" class="copybut" id="copybut24720" onclick="doCopy('code24720')"><u>复制代码</u></a></span> 代码如下:</div> <div class="codebody" id="code24720"> <br><form name="hehe"> <br><input type="checkbox" name="haha"> <br> </form> <br><form name="hehe2"> <br><input type="checkbox" name="haha"> <br><input type="checkbox" name="haha"> <br> </form> <br><script type="text/javascript"> <BR>document.write(document.hehe.haha.length); <BR>document.write('<br />'); <BR>document.write(document.hehe2.haha.length); <BR></script> <br> </div> <br>演示 <br><div class="htmlarea"><textarea id="runcode15511"> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <title></title></textarea></div>