0

我有复选框列表

<asp:CheckBoxList ID="cheBoxTypeOfInc" runat="server" onchange="checkBoxchanged()"  CssClass="form-checkbox" Height="39px" Width="493px" RepeatDirection="Horizontal" RepeatColumns="1">
                 <asp:ListItem Value="CommMotVehiAcci">Commercial Motor Vehicle Accident</asp:ListItem>

                    <asp:ListItem Value="EmpInjry">Employee Injury </asp:ListItem>

                </asp:CheckBoxList>

我正在向其中添加更多项目addElement("InciRepo", "Incident Reporting"); ,addElement 的定义如下:

 function addElement(a, b) {

        var tableRef = document.getElementById('<%= cheBoxTypeOfInc.ClientID %>');
        var tableRow = tableRef.insertRow();

        var tableCell = tableRow.insertCell();

        var checkBoxRef = document.createElement('input');
        var labelRef = document.createElement('label');

        checkBoxRef.type = 'checkbox';
        checkBoxRef.id = a;

        labelRef.innerHTML = b;
        checkBoxRef.value = a;
        tableCell.appendChild(checkBoxRef);
        tableCell.appendChild(labelRef);

    }

我想使用 Javascript 读取选定的值作为

function checkBoxchanged()
    {
        debugger;
        var chebox = document.getElementById('<%= cheBoxTypeOfInc.ClientID %>');
        var checkboxesChecked = [];
        // loop over them all
        for (var i = 0; i < checkboxes.length; i++) {
            // And stick the checked ones onto an array...
            if (checkboxes[i].checked) {
                checkboxesChecked.push(checkboxes[i]);
            }
        }

但它没有给我清单。事实上,当我在寻找选定的项目时,它也没有显示检查。我错过了什么吗?有没有其他方法可以激活这个?请指教 !!

4

1 回答 1

1

这就是我解决它的方法。

            var chebox = document.getElementById('<%= cheBoxTypeOfInc.ClientID %>');
        var options = chebox.getElementsByTagName('input');
        var listOfLabel = chebox.getElementsByTagName('Label');

        for (var i = 0; i < options.length; i++) {
            if (options[i].checked) {
                listOfLabel[i].textContent;
}}

希望这有帮助!

于 2018-09-20T18:58:47.880 回答