如何在asp.net中使用javascript获取所选复选框列表项的值

我正在开发一个asp.net项目,其中我有一个我已经绑定的复选框列表

DataTable dt = new Process_Hotels().SelectAllFacilty(); if (dt.Rows.Count > 0) { cblHotelFacility.DataSource = dt; cblHotelFacility.DataTextField = "Facility"; cblHotelFacility.DataValueField = "ID"; cblHotelFacility.DataBind(); foreach (ListItem li in cblHotelFacility.Items) { li.Attributes.Add("JSvalue", li.Value); } } 

现在我想在按钮点击时使用javascript获取checkboxlist的选定值ID。为此,我在按钮上有以下javascript代码点击:

  function test() { var checkList1 = document.getElementById(''); var checkBoxList1 = checkList1.getElementsByTagName("input"); var checkBoxSelectedItems1 = new Array(); for (var i = 0; i < checkBoxList1.length; i++) { if (checkBoxList1[i].checked) { checkBoxSelectedItems1.push(checkBoxList1[i].value); //alert('checked:' + checkBoxSelectedItems1.push(checkBoxList1[i].getAttribute("JSvalue")).value); alert('checked - : ' + checkBoxList1[i].value) } } }  

但点击按钮所选复选框列表显示0.我想获取所选复选框列表项的ID。请帮助。

试试这个 :

  

尝试调试

 for (var i = 0; i < checkBoxList1.length; i++) { console.log(checkBoxList1[i]) if (checkBoxList1[i].checked) { checkBoxSelectedItems1.push(checkBoxList1[i].value); //alert('checked:' + checkBoxSelectedItems1.push(checkBoxList1[i].getAttribute("JSvalue")).value); alert('checked - : ' + checkBoxList1[i].value) } } 

检查以查看id console.log()通过在控制台窗口按F12为您提供有关该对象的任何信息。 为Firefox安装firebug插件。

我是javascript的新手

这段代码可以帮助你

 function CheckBoxCheckOrNot(jobskill) { var c = document.getElementById(jobskill).getElementsByTagName('input'); for (var i = 0; i < c.length; i++) { if (c[i].type == 'checkbox') { if (c[i].checked) { alert('checkbox checked'); } else { alert('checkbox unchecked'); } } } 

}

注意:jobskill是包含所有复选框的容器ID。