首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >更改CheckBoxList项样式

更改CheckBoxList项样式
EN

Stack Overflow用户
提问于 2016-03-21 11:22:18
回答 1查看 1K关注 0票数 2

我在ASP.NET中有CheckBoxList的应用程序:

代码语言:javascript
复制
<asp:CheckBoxList runat="server" ID="myCheckBoxList">
    <asp:ListItem Text="1 16" />
    <asp:ListItem Text="1 17" />
    <asp:ListItem Text="2 20" />
</asp:CheckBoxList>

单击按钮后,我要更改选定元素的样式。要做到这一点,我有JavaScript功能:

代码语言:javascript
复制
function changeColor() {
    var checkBoxList = document.getElementById("myCheckBoxList");
    var options = checkBoxList.getElementsByTagName('input');

    for (var i = 0; i < options.length; i++) {
        console.log(options[i].checked);
        if (options[i].checked) {
            options[i].parentElement.className = 'Red';
        }
    }
}

当我单击按钮时,选定的项目会在很短的时间内改变它们的颜色(大约0,5秒),然后它们返回到默认的黑色。为什么我的复选框列表项目样式重置?我不想这样做。我如何更改我的代码,使其永久地改变颜色(不仅仅是0,5秒)?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-03-21 12:28:42

以下是完整的解决方案:

代码语言:javascript
复制
<script type="text/javascript">
    function changeColor() {
        var checkBoxList = $('[id$="myCheckBoxList"]');
        var options = checkBoxList[0].getElementsByTagName('input');

        for (var i = 0; i < options.length; i++) {
            console.log(options[i].checked);
            if (options[i].checked) {
                options[i].parentElement.className = 'Red';
            }
        }

        return false;
    }
</script>

<asp:CheckBoxList runat="server" ID="myCheckBoxList">
    <asp:ListItem Text="1 16" />
    <asp:ListItem Text="1 17" />
    <asp:ListItem Text="2 20" />
</asp:CheckBoxList>

<asp:Button OnClientClick="return changeColor();" Text="Test" runat="server" /> 

我已经使用myCheckBoxList本地化了jQuery,因为如果您使用母版页,它可以在浏览器中具有不同的Id。其次,我从函数中返回false,以防止回发按钮点击以维护类值。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36129717

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档