【问题标题】:Set background colour of select to selected option in JQuery将选择的背景颜色设置为 JQuery 中的选定选项
【发布时间】:2011-07-01 09:14:12
【问题描述】:

跟进这个问题:Setting background-color of select options in 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>

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

我通过以下 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');
            }
        }
    );

效果很好。

我还希望选择元素具有与所选选项相同的背景颜色,我正在尝试使用以下方法来实现:

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

但是,这只是将选择元素着色为蓝色(我认为它是从悬停属性而不是背景中获取颜色)。这是在 Firefox 3.6.8 中,就本问题而言,它是唯一相关的浏览器。

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

【问题讨论】:

    标签: jquery css


    【解决方案1】:

    我喜欢@Arnar Yngvason 的回答,但我会将其添加到组合中。此解决方案使用 CSS 类,因此可以轻松更改样式而不会破坏其余代码。

    HTML

    <select>
        <option class="Red">Red</option>
        <option class="Blue">Blue</option>
        <option class="Green">Green</option>
        <option class="Yellow">Yellow</option>
    </select>
    <div id="output"></div>
    

    CSS

    .Red{background-color:#ff0000;}
    .Blue{background-color:#0000ff;}
    .Green{background-color:#00ff00;}
    .Yellow{background-color:#ffff00;}
    

    JavaScript

    $("select").change(function(){
        $(this).removeClass($(this).attr('class'))
               .addClass($(":selected",this).attr('class')); 
        /*If you want to keep some classes just add them back here*/
        //Optional: $(this).addClass("Classes that should always be on the select");
    
    });
    

     $("select").change(function() {
       $(this).removeClass($(this).attr('class'))
         .addClass($(":selected", this).attr('class'));
       /*If you want to keep some classes just add them back here*/
       //Optional: $(this).addClass("Classes that should always be on the select");
    
     });
        .Red {
          background-color: #ff0000;
        }
        .Blue {
          background-color: #0000ff;
        }
        .Green {
          background-color: #00ff00;
        }
        .Yellow {
          background-color: #ffff00;
        }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <select>
      <option class="Red">Red</option>
      <option class="Blue">Blue</option>
      <option class="Green">Green</option>
      <option class="Yellow">Yellow</option>
    </select>
    <div id="output"></div>

    JSFiddle

    我已经在 FireFox 31、IE 11 和 Chrome 37 上对此进行了测试

    【讨论】:

      【解决方案2】:

      将“每个”替换为“更改”

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

      这适用于 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();
      

      最后编辑:

      如果你使用 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>
      

      又一次修改:

      我遇到了这个,发现我可以让它更短,所以我只是为了好玩:

      $('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();
      

      【讨论】:

      • 非常感谢 - 这个答案不仅完美,而且参考资料全面完整。
      【解决方案3】:
      $("select[id$=-status][id^=id_item-]").change(function (){
          var style = $(this).find('option:selected').attr('style');
          $(this).attr('style', style);
          $("select[id$=-status][id^=id_item-] option:selected").each(function () {
                  $(this).attr('style', style);
                });
      }).trigger('change');
      

      为您的 HTML 代码添加样式,使用触发器不要忘记将其加载到您的 $(document).ready(function(){} 另外不要忘记在从数据库中填充您的选择框后放置此代码

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-08-04
        • 2011-10-26
        • 2012-08-08
        • 1970-01-01
        • 2011-08-28
        • 1970-01-01
        • 2017-10-27
        相关资源
        最近更新 更多