3 回答

TA貢獻1827條經驗 獲得超9個贊
您可以使用帶有屬性選擇器的結尾來隱藏所有具有 id 結尾的非匹配表_table
,請參閱下面的代碼
$('#unit_table').hide();
$('#program_levels').change(function(){
//hide all tables
$("table[id$='_table'").hide();
//show all matching tables only
//if($("#" + this.value + "_table").is(":hidden")){ // if condition not required
$("#" + this.value + "_table").show();
//}
});

TA貢獻2041條經驗 獲得超4個贊
你也可以試試這個。
$(document).ready(function(){
$(".jsTable tr").hide();
$(".jsSelect").change(function(){
var oVal = $(this).val();
$(".jsTable tr").hide();
if(oVal!="")
{
$(".jsTable tr[id="+oVal+"]").show();
}
})
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<html>
<body>
<h2>Show hide</h2>
<select class="jsSelect">
<option value="">Select</option>
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
<option value="D">D</option>
</select>
<br/>
<table class="jsTable">
<tr id="A">
<td>Alfreds Futterkiste</td>
<td>Maria Anders</td>
<td>Germany</td>
</tr>
<tr id="B">
<td>Centro comercial Moctezuma</td>
<td>Francisco Chang</td>
<td>Mexico</td>
</tr>
<tr id="C">
<td>Ernst Handel</td>
<td>Roland Mendel</td>
<td>Austria</td>
</tr>
<tr id="D">
<td>Island Trading</td>
<td>Helen Bennett</td>
<td>UK</td>
</tr>
</table>
</body>
</html>
添加回答
舉報