javascript - 选中单选按钮时显示表格
问题描述
有 20 多个字段,如“图像 1”。如果选择是,则显示类似于“图像 2”的表格。所以我有 20 个是/否字段和 20 个不同的表。如果选择是,我如何显示表格。我已经为单个字段尝试了一些代码。由于有很多领域我想知道是否有任何方法可以使代码最小化和更容易。这是我尝试过的代码:
CSS:
#show-dc-table {
display: none;
}
脚本:
<script>
$(document).ready(function() {
$('.form-check-inline input[type="radio"]').click(function() {
if ($(this).attr('id') == 'allergy-yes') {
$('#show-dc-table').show();
} else {
$('#show-dc-table').hide();
}
});
});
</script>
HTML:
<div class="form-group row">
<label class="col-sm-2 col-form-label">Do you have Allergies </label>
<div class="col-sm-10">
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="allergy" value="Yes" id="allergy-yes">
<label class="form-check-label">Yes</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="allergy" value="No">
<label class="form-check-label">No</label>
</div>
</div>
</div>
<table class="table table-striped" id="show-dc-table">
<tr>
<th scope="col">Alergic Reactions to</th>
<th scope="col">Yes</th>
<th scope="col">No</th>
<th scope="col">Notes</th>
</tr>
</table>
解决方案
要使相同的代码适用于多个重复的 HTML 结构,请按行为对每个元素使用相同的类。然后在某些事件发生时使用 DOM 遍历将元素相互关联。
在您的情况下,您将使用closest()
和next()
查找table
与更改的收音机相关的内容。另请注意,您应该使用checked
收音机的属性以及change
事件来确定选择的值。试试这个:
$(document).ready(function() {
$('.form-check-inline input[type="radio"]').on('change', function() {
$(this).closest('.form-group').next('table').toggle(this.checked && this.value === 'Yes');
});
});
.show-dc-table {
display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="form-group row">
<label class="col-sm-2 col-form-label">Do you have allergies?</label>
<div class="col-sm-10">
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="allergy" value="Yes">
<label class="form-check-label">Yes</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="allergy" value="No">
<label class="form-check-label">No</label>
</div>
</div>
</div>
<table class="table table-striped show-dc-table">
<thead>
<tr>
<th scope="col">Alergic Reactions to</th>
<th scope="col">Yes</th>
<th scope="col">No</th>
<th scope="col">Notes</th>
</tr>
</thead>
<tbody>
<tr>
<td>Aspirin, Ibuprofen, Codeine</td>
<td><input type="radio" name="a1" /></td>
<td><input type="radio" name="a2" /></td>
<td><input type="text" /></td>
</tr>
</tbody>
</table>
<div class="form-group row">
<label class="col-sm-2 col-form-label">Do you have a cough?</label>
<div class="col-sm-10">
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="cough" value="Yes">
<label class="form-check-label">Yes</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="cough" value="No">
<label class="form-check-label">No</label>
</div>
</div>
</div>
<table class="table table-striped show-dc-table">
<thead>
<tr>
<th scope="col">Alergic Reactions to</th>
<th scope="col">Yes</th>
<th scope="col">No</th>
<th scope="col">Notes</th>
</tr>
</thead>
<tbody>
<tr>
<td>Lorem ipsum</td>
<td><input type="radio" name="a1" /></td>
<td><input type="radio" name="a2" /></td>
<td><input type="text" /></td>
</tr>
</tbody>
</table>
推荐阅读
- c++ - 帮助理解复制构造函数
- excel - 如何在粘贴信息和签名后放置正文?
- java - 通过确保实体存在来更新实体的最佳方法是否是 Spring Data JPA 中 getOne() 和 save() 的组合?
- read-the-docs - 是否可以为我的文档创建讨论页面?
- java - 无法将数据写入服务器端的多个 Firestore 项目
- url - Alfresco V5.2 - 如何在实时搜索 webscript 中获取当前页面 url
- reactjs - 将对 DOM 元素的引用传递给 React
- sql-server-2012 - 将假期天数添加到日期之间
- java - 从 gitlab 克隆存储库后,intellij 无法识别我的 java 文件
- encryption - 使用椭圆曲线密码术加密数据