javascript - 限制对特定表 ID 的更改
问题描述
我正在尝试为 html 表做一个非常简单的复选框输入。我的页面中有 3 个表格,如果我取消选中“.PrenomTCG”会出现问题,它会将所有结果(Prenom)隐藏在 3 个表格中。
那么,是否可以在我的 jQuery 函数中分配一个 id ?例如“prenomTable”
var $ = jQuery;
// Checkbox
$(document).ready(function () {
// Prénom TCG
$('#prenomEpiques').change(function () {
if (this.checked)
$('.prenomEpique').fadeIn('slow');
else
$('.prenomEpique').fadeOut('slow');
});
$('#prenomEpiques').change();
// Prénom TCG
$('#prenomTCG').change(function () {
if (this.checked)
$('.prenomTCG').fadeIn('slow');
else
$('.prenomTCG').fadeOut('slow');
});
$('#prenomTCG').change();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label class="checkbox-inline">
<input type="checkbox" id="prenomEpiques" class="input_checkbox" checked>When unchecked, I want to hide only rows in the <b>Table 1</b>.
</label><br/>
<label class="checkbox-inline">
<input type="checkbox" id="prenomTCG" class="input_checkbox" checked>When unchecked, I want to hide only rows in the <b>Table 2</b>.
</label>
<h1>Table 1</h1>
<table id="tableauPrenoms1">
<thead>
<tr id="header">
<th>Nom</th>
<th>Prénom</th>
</tr>
</thead>
<tbody>
<tr class="prenomTCG">
<td>Jean</td>
<td>Pierre</td>
</tr>
<tr class="prenomEpique">
<td>Michel</td>
<td>Francis</td>
</tr>
</tbody>
</table>
<h1>Table 2</h1>
<table id="tableauPrenoms2">
<thead>
<tr id="header">
<th>Nom</th>
<th>Prénom</th>
</tr>
</thead>
<tbody>
<tr class="prenomTCG">
<td>Françoise</td>
<td>Léa</td>
</tr>
<tr class="prenomEpique">
<td>Helene</td>
<td>Marie</td>
</tr>
</tbody>
</table>
解决方案
使用更具体的选择器#tableauPrenoms1 .prenomEpique
.
var $ = jQuery;
// Checkbox
$(document).ready(function() {
// Prénom TCG
$('#prenomEpiques').change(function() {
if (this.checked)
$('#tableauPrenoms1 .prenomEpique').fadeIn('slow');
else
$('#tableauPrenoms1 .prenomEpique').fadeOut('slow');
});
$('#prenomEpiques').change();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label class="checkbox-inline">
<input type="checkbox" id="prenomEpiques" class="input_checkbox" checked>When unchecked, I want to hide only rows in the Table 1.
</label>
<h1>Table 1</h1>
<table id="tableauPrenoms1">
<thead>
<tr id="header">
<th>Nom</th>
<th>Prénom</th>
</tr>
</thead>
<tbody>
<tr class="">
<td>Jean</td>
<td>Pierre</td>
</tr>
<tr class="prenomEpique">
<td>Michel</td>
<td>Francis</td>
</tr>
</tbody>
</table>
<h1>Table 2</h1>
<table id="tableauPrenoms2">
<thead>
<tr id="header">
<th>Nom</th>
<th>Prénom</th>
</tr>
</thead>
<tbody>
<tr class="">
<td>Françoise</td>
<td>Léa</td>
</tr>
<tr class="prenomEpique">
<td>Helene</td>
<td>Marie</td>
</tr>
</tbody>
</table>
推荐阅读
- php - 如何以正确的方式在 foreach 中实现 REGEXP?
- flutter - 库比蒂诺相当于“材料”
- flutter - 颤振http.get
- bazel - 使用 Rules Foreign CC 和 Bazel 构建 AWS C++ SDK
- python - 循环使用另一个不同大小的列表压缩列表?
- python-3.x - ModuleNotFoundError:没有名为“speech_recognition”的模块 - Linux
- c++ - std::bind() 不绑定参数
- javascript - 如何使用plotly js在y轴上为单个x轴值(可能是日期)绘制两个点
- php - .htaccess 文件在本地主机上工作,但在服务器上部分工作
- python - 带有多个子字符串的“in”语句