gpt4 book ai didi

javascript - jquery 从几个多重选择中切换

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:58:32 25 4
gpt4 key购买 nike

我有一个表,我想过滤未从多选中选择的行。

<table id="myTable">
<tr>
<td>Working Visa</td>
<td>Blond</td>
</tr>
<tr>
<td>Student Visa</td>
<td>Brown</td>
</tr>
</table>

我想,当我从其中一个选择中选择(或取消选择)值时,它会过滤未选择值的行。

<select class="select" multiple>
<option value="Working Visa"></option>
<option value="Student Visa"></option>
</select>
<select class="select" multiple>
<option value="Blond"></option>
<option value="Brown"></option>
</select>

到目前为止我做了什么

$("select.select").change( function() {

$(this).each(function() {
var valeurs = $(this).val();
//console.log(valeurs);
$("#MyTable tr").show();
$("td").not("td."+valeurs).parent("tr").hide();
});

});

当点击“工作签证”时,我希望只显示带有“工作签证”的行。但是,如果我也单击 Blond,将显示只有工作签证和 Blond 的行。

最佳答案

确定已选择选项的select元素的数量:

var num= $('select.select').has('option:selected').length;

隐藏所有行:

  $('#myTable tr').hide();

仅显示匹配所选选项的 td 等于步骤 1 中的数字的行。

  $('#myTable tr').each(function() {
if(num === $(this).find('td').filter(function() {
return $('option:selected:contains("'+$(this).text()+'")').length;
}).length
) {
$(this).show();
}
});

即使您添加了额外的 select 元素和表格列,这仍然有效。

$('select.select').change(function() {
var num= $('select.select').has('option:selected').length;

$('#myTable tr').hide();

$('#myTable tr').each(function() {
if(num === $(this).find('td').filter(function() {
return $('option:selected:contains("'+$(this).text()+'")').length;
}).length
) {
$(this).show();
}
});
});
#myTable tr {
display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select class="select" multiple>
<option value="Working Visa">Working Visa</option>
<option value="Student Visa">Student Visa</option>
</select>
<select class="select" multiple size="5">
<option value="Blond">Blond</option>
<option value="Black">Black</option>
<option value="Brown">Brown</option>
<option value="Auburn">Auburn</option>
</select>

<table id="myTable">
<tr>
<td>Working Visa</td>
<td>Blond</td>
</tr>
<tr>
<td>Working Visa</td>
<td>Black</td>
</tr>
<tr>
<td>Student Visa</td>
<td>Brown</td>
</tr>
<tr>
<td>Student Visa</td>
<td>Auburn</td>
</tr>
</table>

关于javascript - jquery 从几个多重选择中切换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33851317/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com