从所选选项动态添加到TR类值

时间:2012-11-25 08:10:45

标签: jquery

可能是一件容易的事,但我找不到合适的解决方案?

我需要将一个选定选项的类值添加到相对TR:

<table>
<tr class="odd views-row-first">
<td class="views-field views-field-website-id">1</td>
<td class="views-field views-field-changed">1349422708</td>
<td class="views-field views-field-created">1349422708</td>
<td class="views-field views-field-title">Double field</td>
<td class="views-field views-field-field-rules-conditional  views-field-editablefield">
<div id="editablefields-field-rules-conditional" class="editablefield-item">

<select id="edit-field-rules-conditional-0-field-rules-conditional-und" name="field_rules_conditional[0][field_rules_conditional][und]" class="form-select">
<option value="_none">- None -</option>
<option value="green">green</option>
<option value="red">red</option>
<option value="blue" selected="selected">blue</option>
</select>

</div>         
</td>
</tr>
</table>​

我想要的是:

<tr class="odd views-row-first blue">

1 个答案:

答案 0 :(得分:1)

index函数将解决您的问题。以下内容:

  $("#edit-field-rules-conditional-0-field-rules-conditional-und option").click(function(){
         var index = $("option",$("#edit-field-rules-conditional-0-field-rules-conditional-und")).index($(this));
        var $td = $("#tableId tr:eq(0) td:eq(" + index + ")");            
        $td.addClass($(this).attr("value"));
    });

修改:给你的表一个Id。我在示例中使用了“tableId”,所以用你想要的替换。