简体   繁体   English

如何从下拉列表中替换表格单元格值?

[英]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.

相关问题 Angular:如何获取表中每个单元格都有下拉列表的列,并在每个下拉列表中设置不同的值? - Angular: How do I get a column in a table with a dropdown in each cell and set different values in each of these dropdowns? 如何访问表格单元格中Dropdown的值 - How to acces values of Dropdown in a table cell 如何仅从表的列(而不是标题)获取行的单元格值? - How do I just get the cell values of the rows from a column (and not the header) of the table? 如何从下拉列表中对表格进行排序 - How do I sort table from dropdown list 如何使用JavaScript从表中获取单元格值? - How do I get the cell value from a table using JavaScript? 如何使用jquery或DOM从HTML表中获取单元格? - How do i get a cell from a HTML table with jquery or DOM? 如何检查与单击的表格单元格相邻的值,并在“空”值时替换该值 - How to check for values adjacent to clicked table cell and replace that value if it is “empty” 如何从数据表中获取单元格值? 单元格值 - How to Get cell values from data table ? Cell values 如何从行中的下拉列表中单击的jQuery数据表中的单元格获取值 - How do i get a value from a cell in jquery datatables clicking on dropdown inside a row 如何从下拉列表中获取选定的值以显示在顶部并替换 Header? - How Do I Get the Selected Value from a Dropdown to Appear at the Top and Replace the Header?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM