无需生成动态 ID(我假设您将此 ID 附加到行元素 - tr)。
无论如何,我已经分叉了你的小提琴并创建了一个示例,说明如何根据选择值(每行)更改颜色。
你的行:
<tr>
<td class="some-text"><span class="color-option-a">Some Text</td>
<td>
<select class="color-changer">
<option value="excluded" selected="selected">Excluded</option>
<option value="Other">Other</option>
<option value="Alternate">Alternate</option>
</select>
</select>
</tr>
CSS:
.color-option-a {
红色;
}
.color-option-b {
color: green;
}
.color-option-c {
color: blue;
}
jQuery:
$('.color-changer').on('change', function(){
var currentRow = $(this).closest('tr');
var targetColumn = $('.some-text', currentRow);
var targetSpan = $('span', targetColumn);
var newColor = 'color-option-a';
switch($(this).val()) {
case 'Alternate':
newColor = 'color-option-b';
break;
case 'Other':
newColor = 'color-option-c';
break;
case 'excluded':
newColor = 'color-option-a'; //default
break;
}
//reset the color to none first and then apply the new one
targetSpan.attr('class', '');
targetSpan.addClass(newColor);
});
http://jsfiddle.net/Qccba/2/
希望能帮助到你!