Set background colour of select to selected option in JQuery

前端 未结 3 1186
忘掉有多难
忘掉有多难 2020-12-05 20:27

Follow-up to this question: Setting background-color of select options in JQuery

I have a page with multiple select boxes, as per the following:

<         


        
3条回答
  •  一整个雨季
    2020-12-05 20:52

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

    Add style to you HTML code, use trigger don't forget to load this in your $(document).ready(function(){} Also don't forget to put this code after populating your Select box from the DB

提交回复
热议问题