在JQuery中将select的背景颜色设置为选定选项

mee*_*eep 12 css jquery

此问题的后续行动:在JQuery中设置选择选项的背景颜色

我有一个包含多个选择框的页面,如下所示:

<select name="item-0-status" id="id_item-0-status">
<option value="">---------</option>
<option value="1">Online</option>
<option value="2">Offline</option>
<option value="3">Unknown</option>
</select>
Run Code Online (Sandbox Code Playgroud)

这些是在django中自动生成的,因此无法将css类,id或属性直接应用于选项.select元素包含'item-0-status','item-1-status','item-2-status'等ID.

我通过以下JQuery代码为选项分配颜色:

$('select[id$=-status][id^=id_item-]').children().each(
        function (){
            if($(this).val() == 0){
                $(this).css('backgroundColor','white');
            }
            if($(this).val() == 1){
                $(this).css('backgroundColor','green');
            }
            if($(this).val() == 2){
                $(this).css('backgroundColor','red');
            }
            if($(this).val() == 3){
                $(this).css('backgroundColor','orange');
            }
        }
    );
Run Code Online (Sandbox Code Playgroud)

哪个工作正常.

我还希望select元素与所选选项具有相同的背景颜色,我试图使用以下内容实现:

$('select[id$=-status][id^=id_item-]').each(
        function (){
            $(this).css('backgroundColor',$('option:selected',this).css('backgroundColor'));
        }
    );
Run Code Online (Sandbox Code Playgroud)

但是,这只是将选择元素用蓝色着色(我认为它是从悬停属性而不是背景中获取颜色).这是在Firefox 3.6.8中,出于此问题的目的,它是唯一关注的浏览器.

知道如何解决这个问题吗?

dem*_*mux 20

用"更改"替换"每个"

$('select[id$=-status][id^=id_item-]').change(
    function (){
        var color = $('option:selected',this).css('background-color');
        $(this).css('background-color',color);
    }
).change();
Run Code Online (Sandbox Code Playgroud)

这适用于Chrome.

另见:http://docs.djangoproject.com/en/dev/ref/contrib/admin/#modeladmin-media-definitions

支持django admin中的自定义css.

我相信正确的css属性是:'background-color'. backgroundColor是javascript,应该像这样使用: $(this).css({backgroundColor:color});但它似乎工作,所以没有大问题.

编辑:

如果要在页面加载时初始化脚本,可以在后面添加.change().见代码.

我也在firefox中测试了这个,我也看到了这种奇怪的行为(蓝色,蓝色?).

另一个编辑:

好的,所以这里是firefox的快速修复:

$('select[id$=-status][id^=id_item-]').children().each(function (){
    if($(this).val() == 0){
        $(this).attr('style', 'background-color:white;');
    }
    if($(this).val() == 1){
        $(this).attr('style', 'background-color:green;');
    }
    if($(this).val() == 2){
        $(this).attr('style', 'background-color:red;');
    }
    if($(this).val() == 3){
        $(this).attr('style', 'background-color:orange;');
    }
});

$('select[id$=-status][id^=id_item-]').change(function (){
    var style = $(this).find('option:selected').attr('style');
    $(this).attr('style', style);
}).change();
Run Code Online (Sandbox Code Playgroud)

最后编辑:

如果你使用css,你甚至可以这样做:

<style>
    select option,
    select {
        background-color:white;
    }

    select option[value="1"],
    select.o1
    {
        background-color:green;
    }

    select option[value="2"],
    select.o2
    {
        background-color:red;
    }

    select option[value="3"],
    select.o3
    {
        background-color:orange;
    }
</style>

<script>    
    $('select[id$=-status][id^=id_item-]').change(function (){
        var color = $(this).find('option:selected').val();

        $(this).removeClass('o1 o2 o3').addClass('o' + $(this).find('option:selected').val());
    }).change();
</script>
Run Code Online (Sandbox Code Playgroud)

又一个编辑:

我遇到了这个,看到我可以缩短它,所以我只是为了好玩:

$('select[id$=-status][id^=id_item-]').children().each(function() {    
    var colors = ['white', 'green', 'red', 'orange'];
    $(this).attr('style', 'background-color:' + colors[$(this).val()] + ';');
});
$('select[id$=-status][id^=id_item-]').change(function() {
    $(this).attr('style', $(this).find('option:selected').attr('style'));
}).change();
Run Code Online (Sandbox Code Playgroud)

  • 非常感谢 - 这个答案不仅完美,而且全面而且完整. (2认同)