jquery从checkboxlist上的selected复选框中获取序列号

时间:2013-02-25 10:29:19

标签: jquery .net

这是我的代码:

$('#CheckBoxList1').change(function () {
                var CHK = document.getElementById("<%=CheckBoxList1.ClientID%>");
                var checkbox = CHK.getElementsByTagName("input");
                if ($.browser.msie) {
                    for (var i = 1; i < checkbox.length; i++) {
                        if (checkbox[i].checked) {
                            markers[i - 1].setIcon('http://maps.google.com/mapfiles/ms/icons/green-dot.png');
                        }
                        else {
                            markers[i - 1].setIcon('http://maps.google.com/mapfiles/ms/icons/red-dot.png');
                        }

                    }
                }
          });

        });

在for循环中我找到复选框列表中的复选框。我正在设置标记颜色,在chekboxlist中检查名称。使用for循环工作成功。但是当我选中一个复选框时,我将进行此过程。我不会使用for循环查找所有选中的复选框。所以我会这样做,当我选中chekboxlist上的复选框时,我将获取复选框的索引(序列号)编号当我得到索引号时,我没有使用for循环,我可以处理带有索引号的标记。即 我有一个复选框列表,它有5个复选框,tahats texes是a,b,c,d,e。例如:当我选中'e'复选框时,我将获得该复选框索引号为5,当我cheked'c'复选框时,我将获得该复选框索引号为3

1 个答案:

答案 0 :(得分:1)

只需使用index()函数即可。请注意,某些IE浏览器可能无法识别.change()事件,而是使用.propertychange,如图所示here

另外,当你仍然使用document.getElementById选择你的元素时,使用jQuery的重点是什么?

$(function() {
    $('#CheckBoxList1 input').change(function() {
        var index = $('#CheckBoxList1 input').index(this);
    });
});