In this code, it originated with just a blank table with a select tag:
相关标签: 4条回答 难免孤独 2021-01-26 18:44 Based on your other post that got deleted, I think this is what you're after: $(document).ready(function () { $('#e1').select2(); $('#e2').select2().on('change', function(){ $(this).next().find('.select2-selection').css({ backgroundColor: this.value }); }).trigger('change'); }); Just listen for the change event and update the CSS then. Here's an updated fiddle from your other post: https://jsfiddle.net/jmarikle/ea7q41k7/ 0 讨论(0) 发布评论: 提交评论 加载中... 遇见更好的自我 2021-01-26 18:44 this is an example put it in each function or modify it to whatever you wanna do. $('#e2').on('click', function(){ var title = $(this).attr('title'); $(this).css('background-color', title); }); I have built it on val() but I am sure you can change it. JSFIDDLE HERE 0 讨论(0) 发布评论: 提交评论 加载中... 心在旅途 2021-01-26 18:56 Given the HTML structure which you state that Select2 is generating, your selector is incorrect. The select2-selection and select2-selection--single are both applied to the same element, so the space between them needs to be replaced with a class selector; .. Also note that you don't need the if statement as the title matches the colour you're setting with .css(). Try this: $('.select2-selection.select2-selection--single span.select2-selection__rendered').each(function () { var title = $(this).attr('title'); $(this).parent().css('background-color', title); }); Working example 0 讨论(0) 发布评论: 提交评论 加载中... 伪装坚强ぢ 2021-01-26 19:00 You missing the . in the selector so, use this, $('.select2-selection.select2-selection--single span.select2-selection__rendered') instead of this $('.select2-selection select2-selection--single span.select2-selection__rendered') 0 讨论(0) 发布评论: 提交评论 加载中... 验证码 看不清? 提交回复 热议问题
Based on your other post that got deleted, I think this is what you're after:
$(document).ready(function () { $('#e1').select2(); $('#e2').select2().on('change', function(){ $(this).next().find('.select2-selection').css({ backgroundColor: this.value }); }).trigger('change'); });
Just listen for the change event and update the CSS then.
Here's an updated fiddle from your other post: https://jsfiddle.net/jmarikle/ea7q41k7/
this is an example put it in each function or modify it to whatever you wanna do.
$('#e2').on('click', function(){ var title = $(this).attr('title'); $(this).css('background-color', title); });
I have built it on val() but I am sure you can change it. JSFIDDLE HERE
Given the HTML structure which you state that Select2 is generating, your selector is incorrect. The select2-selection and select2-selection--single are both applied to the same element, so the space between them needs to be replaced with a class selector; ..
select2-selection
select2-selection--single
.
Also note that you don't need the if statement as the title matches the colour you're setting with .css(). Try this:
if
title
.css()
$('.select2-selection.select2-selection--single span.select2-selection__rendered').each(function () { var title = $(this).attr('title'); $(this).parent().css('background-color', title); });
Working example
You missing the . in the selector so,
use this,
$('.select2-selection.select2-selection--single span.select2-selection__rendered')
instead of this
$('.select2-selection select2-selection--single span.select2-selection__rendered')
('.select2-selection select2-selection--single span.select2-selection__rendered')