Javascript不击中复选框

Javascript Not Hitting Checkboxes?

本文关键字:复选框 Javascript      更新时间:2023-09-26

我正在尝试检查被选中的复选框,并从这些复选框打印一些标签。我花了很长时间才让它工作起来。我的JavaScript函数是这样的:

function buildPrintList() {
    var frmMain = document.frmMain;
    var elPrintList = frmMain.printlist;
    var strTemp = '';
    var i = 0;
    // Put together a list of serials to delete.
    for (i = 0; i < frmMain.elements.length; i++) {
        if (frmMain.elements[i].type == 'checkbox' && frmMain.elements[i].name != 'activeonly') {
            if (frmMain.elements[i].checked)
            strTemp = strTemp + frmMain.elements[i].name + ',';
        }
    }
    // Clean up the list.
    if (strTemp.length > 0) strTemp = strTemp.substr(0, strTemp.length - 1);
    // Save the delete list to a element.
    elPrintList.value = strTemp;
}
function printLocationLabels() {
    var frmMain = document.frmMain;
    var elPrintList = frmMain.printlist;
    var elLabelSize = frmMain.labelsize;
    var strLabelSize = '';
    var strURL = '';
    var strTemp = '';
    var xmlHTTP2 = getXMLHTTPObject();
    // Jump out with an error if the object can't be created.
    if (xmlHTTP2 == null) {
        alert("Your browser does not support AJAX!");
        return;
    }
    // Figure out which printer is selected.
    for (i = 0; i < elLabelSize.length; i++) {
        if (elLabelSize[i].checked) strLabelSize = elLabelSize[i].value;
    }
    // Create the URL.
    //strURL = './processors/printLocationLabels.asp?printlist=' + escape(elPrintList.value) + '&labelsize=' + escape(strLabelSize) + '&id=' + Math.random();
    strURL = './processors/printLocationLabels.asp?printlist=' + escape(elPrintList.value) + '&labelsize=' + escape(strLabelSize) + '&id=' + Math.random();
    xmlHTTP2.onreadystatechange = responseTextErrorCheck;
    xmlHTTP2.open("GET", strURL, true);
    xmlHTTP2.send(null);
}

我的复选框是这样设置的(如果有两个复选框,name字段中的1将增加):

<input type="checkbox" name="123<~>1" style="cursor:hand;" class=bodyText" onclick="buildPrintList();"> 

,另一个像这样:

<input type="checkbox" name="activeonly" value="1" checked class="bodyText">

我的问题是我的buildPrintList函数的这一部分没有被击中…

// Put together a list of serials to delete.
for (i = 0; i < frmMain.elements.length; i++) {
    if (frmMain.elements[i].type == 'checkbox' && frmMain.elements[i].name != 'activeonly') {
        if (frmMain.elements[i].checked) alert("Hello! I am an alert box!!" + strTemp);
        strTemp = strTemp + frmMain.elements[i].name + ',';
    }
}

我放了一个警报说你好,给我看你迄今为止建立的字符串…而且它从不开火……知道为什么吗?

我改变了周围的东西,我让它对页面上的每个元素,类型和名称等发出警报....

这是我改变的....

for (i=0; i<frmMain.elements.length; i++){
        alert(frmMain.elements[i].type + " " +  
        frmMain.elements[i].name + " " + frmMain.elements[i].checked);  
}

这是我得到的…

text - filter - false
select - classes - undefined
复选框- activeonly - true
Text - location - false
按钮- doit - false
Hidden - action - false
Hidden - index - false
Hidden - classes - false
隐藏- printlist - false
Hidden - labelsize - false

        <input type="hidden" name="classs" value="">
        <input type="hidden" name="printlist" value="">
        <input type="hidden" name="labelsize" value="">
        <input type="hidden" name="locations" value="">
        <input type="hidden" name="action" value="add">

所以现在我真的很困惑,因为它甚至不考虑那些其他元素作为复选框…这些隐藏变量不会覆盖已经定义的类型,不是吗?

另一件值得一提的事情是,我调用一个单独的页面来构建和填充表…它很大,但我不想错过任何东西……这就是复选框被创建的地方,这可以解释为什么它没有显示在frmMain.elements....

strTemp = "<table width='100%' align='center' cellpadding='3' cellspacing='0' border='0'>"
    strTemp = strTemp & "<tr>"
        strTemp = strTemp & "<td align='left' valign='bottom' nowrap colspan='7' class='bodyText'>"
            strTemp = strTemp & "<table width='65%' align='left' cellpadding='1' cellspacing='0' border='0' style='border: 1px solid orangered;'>"
                strTemp = strTemp & "<tr>"
                    strTemp = strTemp & "<td width='5%' align='right' valign='middle' nowrap class='bodyText6'><span style='font-weight: bold; color: red;'>Note:</span></td>"
                    strTemp = strTemp & "<td align='left' valign='middle' nowrap class='bodyText6'> - Click in a cell to edit it's contents.</td>"
                strTemp = strTemp & "</tr>"
            strTemp = strTemp & "</table>"
        strTemp = strTemp & "</td>"
        strTemp = strTemp & "<td align='right' valign='bottom' colspan='2' nowrap class='bodyText'>"
            strTemp = strTemp & "<input type='button' name='locationssetup' value='Refresh Table' style='font-size: 8; cursor: hand;' class='bodyText' onMouseOver='this.style.color=""orangered"";' onMouseOut='this.style.color=""black"";' onClick='getLocationsTable();'></input>"
        strTemp = strTemp & "</td>"
        strTemp = strTemp & "<td align='right' valign='bottom' colspan='2' nowrap class='bodyText6'>"
            strTemp = strTemp & "<input type='radio' name='labelsize' value='2x3' style='cursor:hand;'>2x3 Labels</input><br><input type='radio' name='labelsize' value='4x6' checked style='cursor:hand;'>4x6 Labels</input><br>"
            strTemp = strTemp & "<input type='button' name='printlabels' value='Print Labels' style='cursor:hand;' class='bodyText6' onMouseOver='this.style.color=""orangered"";' onMouseOut='this.style.color=""black"";' onClick='if (!confirm(""Do you have the correct size labels in the printer?"")) return false; if (document.frmMain.printlist.value.length == 0) {alert(""No Locations Selected!""); return false;}  else {printLocationLabels();}'></input>"
        strTemp = strTemp & "</td>"
    strTemp = strTemp & "</tr>"
    strTemp = strTemp & "<tr style='background-color: darkorange; font-weight: bold;'>"
        strTemp = strTemp & "<td align='left' valign='bottom' nowrap class='bodyTextTL'>Location</td>"
        strTemp = strTemp & "<td align='center' valign='bottom' nowrap class='bodyTextTL'>Shelf</td>"
        strTemp = strTemp & "<td align='center' valign='bottom' nowrap class='bodyTextTL'>Description</td>"
        strTemp = strTemp & "<td align='center' valign='bottom' nowrap class='bodyTextTL'>Depth</td>"
        strTemp = strTemp & "<td align='center' valign='bottom' nowrap class='bodyTextTL'>Status</td>"
        strTemp = strTemp & "<td align='center' valign='bottom' nowrap class='bodyTextTL'>Class <span style='font-size:10;'>(class description)</span></td>"
        strTemp = strTemp & "<td align='center' valign='bottom' nowrap class='bodyTextTL'>Crossdock</td>"
        strTemp = strTemp & "<td align='center' valign='bottom' nowrap class='bodyTextTL'>Last<br>Cycle Count</td>"
        strTemp = strTemp & "<td align='center' valign='bottom' nowrap class='bodyTextTL'>CC<br>Priority<br><span style='font-size:8px;'>(1 - 10)</span></td>"
        strTemp = strTemp & "<td align='center' valign='bottom' nowrap class='bodyTextTL'>Active</td>"
        strTemp = strTemp & "<td align='center' valign='bottom' nowrap class='bodyTextTLR'>Print<br>Label</td>"
    strTemp = strTemp & "</tr>"
    If rsTemp.EOF Then
        strTemp = strTemp & "<tr height='35'>"
            strTemp = strTemp & "<td align='center' valign='middle' colspan='11' nowrap style='font-weight: bold;'class='bodyTextTLR'>No Data Found</td>"
        strTemp = strTemp & "</tr>"
        strTemp = strTemp & "<tr>"
            strTemp = strTemp & "<td align='center' valign='middle' nowrap colspan='11' style='font-weight: bold;' class='bodyTextT'>&nbsp</td>"
        strTemp = strTemp & "</tr>"
    Else
        Do While Not rsTemp.EOF
            strActive = "Active"
            If rsTemp("active") = 0 Then
                strActive = "Dormant"
            End If
            strTemp = strTemp & "<tr>"
                strTemp = strTemp & "<td align='left' valign='middle' nowrap class='bodyTextTL'>" & Trim(rsTemp("location")) & "&nbsp</td>"
                strTemp = strTemp & "<td align='left' valign='middle' nowrap style='cursor:hand;' class='bodyTextTL' onMouseOver='this.style.color=""red"";' onMouseOut='this.style.color=""black"";' onClick='updateLocationsTable(""" & rsTemp("location") & """,""" & rsTemp("shelf") & """, ""shelf"", 2);'>" & Trim(rsTemp("shelf")) & "&nbsp</td>"
                strTemp = strTemp & "<td align='left' valign='middle' nowrap style='cursor:hand;' class='bodyTextTL' onMouseOver='this.style.color=""red"";' onMouseOut='this.style.color=""black"";' onClick='updateLocationsTable(""" & rsTemp("location") & """,""" & rsTemp("description") & """, ""description"", 101);'>" & Trim(rsTemp("description")) & "&nbsp</td>"
                strTemp = strTemp & "<td align='center' valign='middle' nowrap style='cursor:hand;' class='bodyTextTL' onMouseOver='this.style.color=""red"";' onMouseOut='this.style.color=""black"";' onClick='updateLocationsTable(""" & rsTemp("location") & """, """ & rsTemp("depth") & """, ""depth"", 2);'>" & Trim(rsTemp("depth")) & "&nbsp</td>"
                strTemp = strTemp & "<td align='left' valign='middle' nowrap style='cursor:hand;' class='bodyTextTL' onMouseOver='this.style.color=""red"";' onMouseOut='this.style.color=""black"";' onClick='updateLocationsTable(""" & rsTemp("location") & """, """ & rsTemp("status") & """, ""status"", 1);'>" & Trim(rsTemp("status")) & "&nbsp</td>"
                strTemp = strTemp & "<td align='left' valign='middle' nowrap style='cursor:hand;' class='bodyTextTL' onMouseOver='this.style.color=""red"";' onMouseOut='this.style.color=""black"";' onClick='updateLocationsTable(""" & rsTemp("location") & """, """ & rsTemp("class") & """, ""class"", 31);'>" & Trim(rsTemp("class")) & " <span style='font-size:10;'>(" & Trim(rsTemp("description2")) & ")</span>&nbsp</td>"
                strTemp = strTemp & "<td align='right' valign='middle' nowrap style='cursor:hand;' class='bodyTextTL' onMouseOver='this.style.color=""red"";' onMouseOut='this.style.color=""black"";' onClick='updateLocationsTable(""" & rsTemp("location") & """, """ & rsTemp("crossdock") & """, ""crossdock"", 2);'>" & Trim(rsTemp("crossdock")) & "&nbsp</td>"
                strTemp = strTemp & "<td align='right' valign='middle' nowrap class='bodyTextTL'>" & Trim(rsTemp("last_count")) & "&nbsp</td>"
                strTemp = strTemp & "<td align='right' valign='middle' nowrap style='cursor:hand;' class='bodyTextTL' onMouseOver='this.style.color=""red"";' onMouseOut='this.style.color=""black"";' onClick='updateLocationsTable(""" & rsTemp("location") & """,""" & rsTemp("cc_required") & """, ""cc_required"", 2);'>" & rsTemp("cc_required") & "&nbsp</td>"
                strTemp = strTemp & "<td align='center' valign='middle' nowrap style='cursor:hand;' class='bodyTextTL' onMouseOver='this.style.color=""red"";' onMouseOut='this.style.color=""black"";' onClick='changeLocationStatus(""" & rsTemp("location") & """, """ & rsTemp("active") & """);'>" & strActive & "&nbsp</td>"
                    strTemp = strTemp & "<td align='center' valign='center' nowrap class='bodyTextTLR'>"
                        'strTemp = strTemp & "<input type='checkbox' name='" & rsTemp("location") & "~" & rsTemp("shelf") & "' style='cursor:hand;' class='bodyText' onClick='buildPrintList();'></input>"
                        strTemp = strTemp & "<input type='checkbox' name='" & rsTemp("location") & "<~>" & rsTemp("shelf") & "' style='cursor:hand;' class='bodyText' onClick='buildPrintList();'></input>"
                    strTemp = strTemp & "</td>"
            strTemp = strTemp & "</tr>"
            rsTemp.MoveNext
        Loop
        strTemp = strTemp & "<tr>"
            strTemp = strTemp & "<td align='right' valign='middle' colspan='11' nowrap style='font-size: 10; font-weight: bold;' class='bodyTextT'>&nbsp</td>"
        strTemp = strTemp & "</tr>"
    End If
    rsTemp.Close
    strTemp = strTemp & "</table>"
    Response.Write(strTemp)

也许你所有的输入元素都被呈现与name="activeonly"或也许变量初始化frmMain = document.frmMain;不是指向你的形式。试试frmMain = document.getElementById( 'frmMain' );,别忘了把id="frmMain"放在form元素