How to access the key of ListItems in CheckBoxList
I need to access the key values of the individual Checkboxes in my CheckBoxList in the client. How do I do this please? The code below works to select the text however, I want to access the integer values as below please.
<asp:CheckBoxList id=rbGender runat="server" Width="200px" RepeatDirection="Horizontal">
<asp:Li开发者_Python百科stItem Value="-1">-1</asp:ListItem>
<asp:ListItem Value="0">Female</asp:ListItem>
<asp:ListItem Value="1">Male</asp:ListItem>
</asp:CheckBoxList>
function getCheckBoxListItemsChecked(elementId) {
var elementRef = document.getElementById(elementId);
var checkBoxArray = elementRef.getElementsByTagName('input');
var checkedValues = '';
for (var i = 0; i < checkBoxArray.length; i++) {
var checkBoxRef = checkBoxArray[i];
if (checkBoxRef.checked == true) {
var labelArray = checkBoxRef.parentNode.getElementsByTagName('label');
if (labelArray.length > 0) {
if (checkedValues.length > 0)
checkedValues += ', ';
checkedValues += labelArray[0].innerHTML;
}
}
}
return checkedValues;
}
function CopyItemsToTextBox() {
var checkedItems = getCheckBoxListItemsChecked('<%= rbGender.ClientID %>');
alert('Items checked: ' + checkedItems);
return checkedItems;
}
try this jQuery code for single element selection
$("input:checked","[id$='rbGender']").val()
and this for multiple. each will loop through each selected checkbox
$.each($("input:checked","[id$='rbGender']"), function(k, v) {
alert(v.value);
});
精彩评论