javascript - 使用选择框突出显示匹配的 html 单元格

标签 javascript jquery

我想用顶部的选择框突出显示具有匹配值的 html 单元格。目前它只突出显示该行。我能得到一点帮助吗?

我在这里尝试做的是,当从选择框中选择一个数字时,具有匹配值的相应单元格将突出显示。我想同时突出显示多个单元格。请注意,它们是两个不同的表。

这是我的 jsfiddle .

这是html代码:

<div id="wrapper">

<table class="table1">

<tr>

<td><select name="list1" id="list1">
        <option value="">List</option>
                <option value="1">1</option>
                <option value="2">2</option>
                <option value="3">3</option>
</select>   
</td>
<td><select name="list2" id="list2">
        <option value="">List</option>
                <option value="11">11</option>
                <option value="12">12</option>
                <option value="13">13</option>
</select>   
</td>

<td><select name="list3" id="list3">
        <option value="">List</option>
                <option value="21">21</option>
                <option value="22">22</option>
                <option value="23">23</option>
</select>   
</td>
</tr>
</table>

<table class="table2">

<tr class="row">
<td value="1"> 1 </td>
<td value="11">11</td>
<td value="21">21 </td>
</tr>

<tr class="row">
<td value="2"> 2 </td>
<td value="12">12 </td>
<td value="22">22 </td>
</tr>

<tr class="row">
<td value="3"> 3 </td>
<td value="13">13 </td>
<td value="23">23 </td>
</tr>

</table>

</div>

CSS 代码:

.table1,td {
    border:1px solid #999;
}

.table1 td {
    width:150px;
}


.table2,td {
    border:1px solid #999;
}

.table2 td {
    width:150px;
}

J查询代码:

$("#list1").change(function () {

   var index = this.value - 1;

    var $rows = $('.row', '.table2');

    $rows.css('background-color', '');

    $('.row', '.table2').eq(index).css('background-color', 'yellow');
});

最佳答案

您可以使用 .each() 来实现此目的迭代所有单元格的方法,在当前 <select /> 上测试每个单元格上的值匹配值(value)。与此类似的内容可能对您有用:

// Apply to all selectors
$("select").change(function () {

  // Extract the index of the select being interacted with
  var selectIndex = $(this).parent().index();
  
  var value = $(this).val();
	
  // Iterate each cell of the table
  $('td', '.table2').each(function() {

    // If the cell index matches the index of the corresponding
    // selected drop down then update it's background color   	
  	if($(this).index() === selectIndex) {
      
      // If a value match is found, apply background color. Other
      // wise clear the background color
      if($(this).attr('value') ==  value) {
        $(this).css('background-color', 'yellow');
      }
      else {
      	$(this).css('background-color', '');
      }
    }
    
  })
});
.table1,td {
    border:1px solid #999;
}

.table1 td {
    width:150px;
}


.table2,td {
    border:1px solid #999;
}

.table2 td {
    width:150px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<body>

<div id="wrapper">

<table class="table1">

<tr>

<td>
    <select name="list1" id="list1">
        <option value="">List</option>
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
    </select>   
</td>
<td>
    <select name="list2" id="list2">
        <option value="">List</option>
        <option value="11">11</option>
        <option value="12">12</option>
        <option value="13">13</option>
    </select>   
</td>
<td>
    <select name="list3" id="list3">
        <option value="">List</option>
        <option value="21">21</option>
        <option value="22">22</option>
        <option value="23">23</option>
    </select>   
</td>
</tr>
</table>

<table class="table2">

<tr class="row">
<td value="1"> 1 </td>
<td value="11">11</td>
<td value="21">21 </td>
</tr>

<tr class="row">
<td value="2"> 2 </td>
<td value="12">12 </td>
<td value="22">22 </td>
</tr>

<tr class="row">
<td value="3"> 3 </td>
<td value="13">13 </td>
<td value="23">23 </td>
</tr>

</table>

</div>

希望对您有所帮助! ( here's a jsfiddle as well )

关于javascript - 使用选择框突出显示匹配的 html 单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52976296/

相关文章:

javascript - jQuery的scrollTo可以在Chrome中使用,但不能在FF中使用

javascript - Sublime 文本查找并替换为正则表达式以将 jquery live() 事件转换为 on()

javascript - 将平铺图 block map 读取为 JSON - 错误

javascript - google chart a.S 不是函数

jquery - 使用 icheck 插件进行 Bootstrap 多选

javascript - 使用 winjs.promise 实现 .done().fail() 方法

javascript - jquery 在后退按钮上填充错误的输入

javascript - 如何定位javascript文件位于dom中的元素

javascript - 删除后减少 JSON 数组中的 ID

javascript - jquery datepicker 多个 css 类