为什么这个用于检查复选框值的javascript循环不起作用?

时间:2015-10-15 08:00:39

标签: javascript html checkbox vbscript asp-classic

JavaScript中的以下表单提交功能将获得第一个复选框值,但无法获取其他值。我已经测试了这么多种方法,并且在第二次获得复选框的“.checked”值时似乎总是存在问题。

使用Javascript:

function checkForm()
{
  numrows = document.theform.numrows.value;
  if(numrows == -1)
  {
    alert("You do not have any transactions setup yet");
    return false;
  }

  emailcheckcount = 0;
  for(i=0; i<=numrows; i++)
  {
      var recid = document.forms["theform"]["recid"+i].value;
      var checkbox = document.getElementById(recid+"_to"+i);

      if (checkbox.checked)
      {
      emailcheckcount++;

      }
  }

    if(emailcheckcount == 0) {
    alert("Please Select At Least One Email for Alerts");
    return false;
    }

  document.theform.submit();
}

HTML / ASP:

<%
    numrows = -1
    strSQL = "select recid, customer, documenttpid, tolist, cclist, bcclist, receivedvia, addmailout, emailsubject from MailOut where customer like '%"&cname&"%' and (receivedvia = 'email' or addmailout = 'email') and emailsubject <> 'ignore' order by customer, documenttpid;"
    Set rs = objConnection2.Execute(strSQL, ,adCmdText)
    if not (rs.eof and rs.bof) Then

      arr = rs.getrows()
      numrows = ubound(arr,2)
      for i=0 to numrows
        dim tolist
        dim cclist

        recid = arr(0,i)
        customer = arr(1,i)
        documenttpid = arr(2,i)
        if not IsNull(arr(3,i)) Then
          tolist = split(arr(3,i),"|")
        else
          tolist = null
        end if


        if not IsNull(arr(4,i)) and Len(arr(4,i)) > 0 Then
          cclist = split(arr(4,i),"|")
        else
          cclist = null
        end if

        bcclist = arr(5,i)
        receivedvia = arr(6,i)
        addmailout = arr(7,i)
        emailsubject = arr(8,i)


    %>
    <tr>
    <!--
    <td valign='top' style="border-bottom:1px solid black;">&nbsp;<%=customer%></td>
    <td valign='top' style="border-bottom:1px solid black;">&nbsp;<%=receivedvia%></td>
    -->
    <td valign='top' style="border-bottom:1px solid black;">&nbsp;<%=documenttpid%></td>
    <td valign='top' style="border-bottom:1px solid black;">
    <input type="hidden" name="recid<%=i%>" value="<%=recid%>">&nbsp;<%=emailsubject%></td>
    <td valign='top' style="border-bottom:1px solid black;">
    <%
    emailnum = 0
    if not isNULL(tolist) then
        for each email in tolist
          if  not(isNULL(email) or trim(email)="") Then
    %>
      <input type='checkbox' id='<%=recid%>_to<%=emailnum%>' name='<%=recid%>_to<%=emailnum%>'>&nbsp;<%=email%> /<%=recid%>_to<%=emailnum%>
      <input type='hidden' name='<%=recid%>_to<%=emailnum%>email' value='<%=email%>'><BR>
    <%
          end if

          emailnum = emailnum + 1
        next
    end if
    %>
    <input type='hidden' name='numtoemails<%=i%>' value='<%=emailnum%>'>

    <BR>Add Email:<input type='text' name='addtoemail<%=i%>'></td>
    <td valign='top' style="border-bottom:1px solid black;">
    <%
    ccnum = 0
    if(not isNULL(cclist))then
        for each email in cclist
          if trim(email) <> "") = 0 Then
    %>
      <input type='checkbox' name='<%=recid%>_cc<%=ccnum%>'>&nbsp;<%=email%>
      <input type='hidden' name='<%=recid%>_cc<%=ccnum%>email' value='<%=email%>'><BR>
    <%
          end if

          ccnum = ccnum + 1
        next
    end if
    %>
    <input type='hidden' name='numccemails<%=i%>' value='<%=ccnum%>'>

    <BR>Add Email:<input type='text' name='addccemail<%=i%>'></td>
    </tr>
    <%
      next
    end if
    %>

    <input type="hidden" name="numrows" value="<%=numrows%>">

    <tr><td align="Right" colspan="4"><input type="button" value="Submit" style="width:70px;" class="btn-primary" onclick="checkForm();"></td></tr>

1 个答案:

答案 0 :(得分:0)

每次有新的recid

时,

emailnum从0开始

   emailnum = 0
...
      <input type='checkbox' id='<%=recid%>_to<%=emailnum%>' name='<%=recid%>_to<%=emailnum%>'>&nbsp;<%=email%> /<%=recid%>_to<%=emailnum%>

但是,你的javascript代码只会增加后缀i

  var checkbox = document.getElementById(recid+"_to"+i);

检查生成的HTML,看它是否符合您的Javascript假设。