2017-08-29 216 views
1

私は、Select2を使用してアプリケーションで複数選択タグタイプフィールドを設定しています。私は、それが選択フィールドに配置されるとCSS属性を選択ボックスの色を変更する知っている:選択フィールド内のselect2選択の背景色を変更する

選択ボックスの色私は何を把握しようとしていますが、この変更を持っている方法です

.select2-container--default .select2-selection--multiple .select2-selection__choice { 
    background-color: #e4e4e4; 
    border: 1px solid #aaa; 
    border-radius: 4px; 
    cursor: default; 
    float: left; 
    margin-right: 5px; 
    margin-top: 5px; 
    padding: 0 5px 
} 

ユーザーが選択した内容に基づいて選択リストはあらかじめ定義されています。これは動作するように表示されません

$(document).ready(function() { 
     $('#test').select2(); 
     $('.select2-container--default .select2-selection--multiple .select2-selection__choice').each(function() { 
      var title = $(this).attr('title'); 
      console.log(title); 
      if (title === 'Breakfast') { 
       $(this).parent().css({ 'background-color': "green" }); 
      } 
      if (title === 'Brunch') { 
       $(this).parent().css({ 'background-color': "red" }); 
      } 
      if (title === 'Lunch') { 
       $(this).parent().css({ 'background-color': "blue" }); 
      } 
     }); 
    }); 

/変更ボックスの色を初期化するために

はJavaScript:

はこのような何かをしようとしました。誰もSelect2用のタグボックスをスタイリングしようとしましたか?

+1

私はスタイルをJavaScriptで設定するのではなく、オーバーライドすることをお勧めします。プラグインはオープンソースなので、スタイルシートはgssubでscssファイル(https://github.com/select2/select2/tree/master/src/scss)として利用できます。ブートストラップを使用している場合は、このリポジトリのファイルを使用してください。https://github.com/select2/select2-bootstrap-theme/tree/master/src – r3bel

答えて

1

$(this).parent().css({ 'background-color': "green" });

によって変更$(this).parent().css('background-color', 'green');

そして残りの部分と同じ。

+0

それはうまくいかないようです。オリジナルのJSを更新しました –

関連する問題