页面:源码太多,删减后部分:
   <table cellspacing="0" class="datatable">
          <tr class="coltitle">
            <td style="height: 23px">选择</td>
          </tr>
        <%  int i = 0;
                IEnumerator ie = listTabInvoice.GetEnumerator();
                string strinovic = "";   
                while (ie.MoveNext())
                {
                    Cas.Entity.NHibernate.TabInvoice obj = (Cas.Entity.NHibernate.TabInvoice)(ie.Current);
                    strinovic += obj.Invoiceid + ",";
                    //hidinovices.Value += obj.Invoiceid + ",";
             %>
          <tr>
            <td style="height: 27px">
                <input type="checkbox" value="<%=obj.Invoiceid%>" id="chkchild" name="groupchk" />
            </td>
          </tr>
          <%
              i++;
          }
          hidinovices.Value = strinovic.TrimEnd(',');
          %>
        </table>    <input type="button" name="Button1"  class="button"  value="合并Invoice"  onclick="AA();" style="width: 158px"/>JS:
function AA(){ 
  var invoices="";
FALSE:  var length1 = document.form1.groupchk.length;。
        alert(length1);//当数据为一条时不能取到值,弹出undefined  TRUE:   var length2=document.getElementsByName("groupchk").length;
        alert(length2);//弹出 1   

问:
①document.form1.groupchk.length和document.getElementsByName("groupchk").length这两种写法的区别?
②当数据为多条时,它们弹出的条数是一样的,当数据为1条时,document.form1.groupchk.length为未定义,为什么?

解决方案 »

  1.   

    当数据多条时document.form1.groupchk和document.getElementsByName("groupchk") 返回的都是一个控件数组,所以都存在length属性,当数据只有一条的时候,document.form1.groupchk返回的是不是数组所以不存在length属性。
      

  2.   

    不知道到底有什么区别。。只知道访问空间两个都可以用。。好像。那个form1是到当前页面用,那个getElementsbyName(“”)床控件的时候可以跨页面。不知道对不对。这是asp中我这样做过。不太记得了。。
      

  3.   

      document.getElementsByName返回的是控件数组
    当一个时document.form1.groupchk返回元素,没有length属性