在“选择”选项中设置所选项目的背景颜色(多个)

问题描述:

我正在尝试更改列表框中所选项目的背景颜色(选择多个选项)。

I'm trying to change the background color of the selected items in a listbox (select multiple options).

我尝试用css ...我尝试使用JavaScript ...并使用jquery ...我真的不知道发生了什么!但是我找不到答案:_(。

I tried with css... I tried with JavaScript... and with jquery... I really don't know what happen!! But I'm unable to find the answer :_(.

我在这个论坛上至少读过10个问题..我尝试了所有提议的解决方案,但我只能做到更改完整列表的背景...而不是选中的项目。我找到的唯一方法是使用带有可点击列表项的无序列表,如果我的选择 - 选项......但我正在使用表单,而我想要以传统方式向服务器发送所选项目。

I read at least 10 questions in this forum.. and I tried all the proposed solutions but I only can change the background of the complete list... not the selected items. The only way I found is to use an unordered list with clickable list items instead if my select - option ... but I'm working with a form, and I want to send to the server the selected items in the traditional way.

这将更改所选选项的背景颜色

This will change the background color of the selected option

$('select').change(function() {
    $('option').css('background', 'none');
    $('option:selected').css('backgroundColor', 'red');
}).change();