[英]How do I replace table cell values from a dropdown?
I need to "massage" a HTML table. 我需要“按摩” HTML表。
On the page there is a dropdown: 在页面上有一个下拉列表:
<SELECT CLASS='slctor' size='1' NAME='KEYVAL' ID='KEYVAL' >
<OPTION VALUE='{*}'>*
</OPTION>
<OPTION value='10'>green
</OPTION>
<OPTION value='20'>green
</OPTION>
<OPTION value='30'>green
</OPTION>
<OPTION value='40'>yellow
</OPTION>
<OPTION value='90'>red
</OPTION>
</SELECT>
I also have a table with Person/year and a value for each pair: 我还有一个表,其中包含“人/年”和每对值:
<table>
<tr>
<td></td>
<td>2001</td>
<td>2002</td>
<td>2003</td>
</tr>
<tr>
<td>JOE</td>
<td>10</td>
<td>30</td>
<td>90</td>
</tr><tr>
<td>BETTY</td>
<td>20</td>
<td>20</td>
<td>40</td>
</tr>
</table>
I want to use jQuery to "resolve" each cell value from the dropdowns value, return the label, and replace the cell content (or the background color). 我想使用jQuery从下拉列表值“解析”每个单元格值,返回标签,并替换单元格内容(或背景颜色)。
I am able to do the basics, as in the actual DOM manipulation, but I need help actually doing the lookup in a efficient manner. 我可以进行基本操作,就像在实际的DOM操作中一样,但是实际上我需要以有效的方式进行查找的帮助。
Try this:
$(".slctor").change(function(){
var selectedVal=$(this).val();
$('table').find('td').filter(function(){return $(this).text() ==selectedVal}).css("background-color","red");
//or
$('table').find('td').filter(function(){return $(this).text() ==selectedVal}).text("New Text");
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.