javascript - 选中复选框后如何更改 DIV 的颜色?

标签 javascript jquery

我想在选中复选框时更改 DIV 的颜色。我想在 jQuery 中做到这一点。我已经尝试过这个:

 if($('#checkboxTest').is(':checked'))
    {
        $('.alertWrapper').css('background-color', 'blue');
    }

但它不起作用......有什么帮助吗?

最佳答案

您需要使用.change() 事件来跟踪复选框的状态:

$('#checkboxTest').change(function() {
    if($(this).is(':checked'))
    {
        $('.alertWrapper').css('background-color', 'blue');
    } else {
        // Your code here will fired when the checkbox is unchecked
    }
}).change(); // <-- Trigger the change event on page load

关于javascript - 选中复选框后如何更改 DIV 的颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22657074/

相关文章:

javascript - 永远循环并提供增量时间

javascript - Backbone : populate multiple drop-downs with the same data?

javascript - 在 Cordova/Ionic iOS 应用程序中使用 WWW-Authenticate 处理 HTTP 401

javascript - 元素在父 div 中的随机位置

javascript - 如何在多个元素上但不同时使用 toggleClass

javascript - 使用 jQuery 将整数添加到 css 类

javascript - RemoveEventListener 不删除事件

javascript - 将 Plot.ly 与外部脚本结合使用来绘制图表

jquery - 从某些元素的标题和第一个子元素中删除 href

jquery - 无法获取对 TinyMCE 编辑器实例的引用