【发布时间】:2018-08-30 23:11:34
【问题描述】:
我有复选框列表
<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]);
}
}
但它没有给我列表。事实上,当我在寻找选定的项目时,它也没有显示检查。 我错过了什么吗?有没有其他方法可以激活这个?请指教!!
【问题讨论】:
-
您的元素选择器似乎在查询错误的 ID。
document.getElementById('<%= cheBoxTypeOfInc.ClientID %>')不是要选择的有效元素 ID。根据您的标记,我相信正确的是:document.getElementById('cheBoxTypeOfInc') -
此外,在选择复选框列表后,您需要遍历它的子项并获取具有选定值的子项。这里有人做过类似的事情:stackoverflow.com/a/21133799/5330050