在复选框列表中选中复选框的索引每次选中复选框时都会更改。 [英] In Checkboxlist The index Of Selected Checkbox Is Change Each Time When I Select A Checkbox.

查看:124
本文介绍了在复选框列表中选中复选框的索引每次选中复选框时都会更改。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望每次在checkboxlist中选择diffrent复选框时,在复选框列表中显示标签中选定的复选框索引。我该怎么办?

解决方案

使用JavaScript,参见示例:

 <   script    类型  =  text / javascript    language   =  javascript >  
function showCheckedBoxId(){
var checkboxlist = document.getElementById('<% = CheckBoxList1.ClientID %> ');
var label = document.getElementById('<% = Label1.ClientID %> );
label.innerHTML =你选择了;
var checkBoxes = checkboxlist.getElementsByTagName(input);
for(i = 0; i < checkBoxes.length; i ++) {

if (checkBoxes [i] .checked) label.innerHTML = label.innerHTML + + checkBoxes [i] .id;

}

return false;

< span class =code-attribute> }

< / script >
< / head >
< body >
< 表格 id = form1 runat = < span class =code-keyword> server >
< div >
< asp:CheckBoxList ID = CheckBoxList1 runat = server onchange = return showCheckedBoxId() >
< asp:ListItem > ASP.NET < / asp:ListItem >
< asp:ListItem > C# < / asp:ListItem >
< asp:ListItem > VB.NET < / asp:ListItem >
< asp:ListItem > PHP < / asp:ListItem >
< asp:ListItem > HTML < / asp:ListItem >
< / asp:CheckBoxList >
< ; br / < span class =code-keyword>>
< asp:Label 我D = Label1 runat = server > < / asp:Label > < / div >
< / form >


i want to show in checkboxlist the selected checkbox index in a label each time when i select diffrent checkbox in checkboxlist. how can i do it?

解决方案

Use JavaScript, see example:

<script type="text/javascript" language="javascript">
        function showCheckedBoxId() {
            var checkboxlist = document.getElementById('<%= CheckBoxList1.ClientID%>');
            var label = document.getElementById('<%= Label1.ClientID%>');
            label.innerHTML = "You have selected ";
            var checkBoxes = checkboxlist.getElementsByTagName("input");
            for (i = 0; i < checkBoxes.length; i++) {

                if (checkBoxes[i].checked) label.innerHTML = label.innerHTML + " " + checkBoxes[i].id;

            }

            return false;

        }

    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:CheckBoxList ID="CheckBoxList1" runat="server" onchange="return showCheckedBoxId()">
            <asp:ListItem>ASP.NET</asp:ListItem>
            <asp:ListItem>C#</asp:ListItem>
            <asp:ListItem>VB.NET</asp:ListItem>
            <asp:ListItem>PHP</asp:ListItem>
            <asp:ListItem>HTML</asp:ListItem>
        </asp:CheckBoxList>
        <br />
        <asp:Label ID="Label1" runat="server"></asp:Label></div>
    </form>


这篇关于在复选框列表中选中复选框的索引每次选中复选框时都会更改。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆