javascript - 隐藏输入/选择时,表单字段标签不会隐藏
问题描述
我已经创建了一个 Bootstrap 表单,并且我在控件上使用了类表单浮动。
当我隐藏一些控件时,标签仍然存在并被挤压在一起。这会发生在某些控件上,但不会发生在其他控件上。代码看起来完全一样,所以我对正在发生的事情有点恼火。
我确实注意到的一件事是,当我将隐藏字段移动到触发隐藏的字段上方时,标签被正确隐藏。但是,如果我将它们移回触发隐藏的字段下,标签将再次保留并重叠。
HTML:
<div class="form-floating mb-4">
<select class="form-select" id="frmProjectInterconnect" required>
<option value="" selected disabled></option>
<option>Yes</option>
<option>No</option>
</select>
<label for="frmProjectInterconnect">Project Interconnection Point</label>
</div>
<div class="form-floating mb-4">
<input type="number" class="form-control" id="frmInterconnectionCapacity" required>
<label for="frmInterconnectionCapacity">Interconnection Capacity (Mbps)</label>
</div>
<div class="form-floating mb-4">
<select class="form-select" id="frmDedicatedOrShared" required>
<option value="" selected disabled> </option>
<option>Dedicated</option>
<option>Shared</option>
</select>
<label for="frmDedicatedOrShared">Dedicated or Shared?</label>
</div>
<div class="form-floating mb-4">
<input type="text" class="form-control" id="frmServiceProvider" required>
<label for="frmServiceProvider">Service Provider</label>
</div>
Javascript:
$('#frmProjectInterconnect').change(function(){
if($(this).val() == "Yes") {
$("#frmInterconnectionCapacity").show();
$("#frmInterconnectionCapacity").attr('required', '');
$("#frmInterconnectionCapacity").attr('data-error', 'This field is required.');
$("#frmDedicatedOrShared").show();
$("#frmDedicatedOrShared").attr('required', '');
$("#frmDedicatedOrShared").attr('data-error', 'This field is required.');
$("#frmServiceProvider").show();
$("#frmServiceProvider").attr('required', '');
$("#frmServiceProvider").attr('data-error', 'This field is required.');
} else {
$("#frmInterconnectionCapacity").hide();
$("#frmInterconnectionCapacity").removeAttr('required');
$("#frmInterconnectionCapacity").removeAttr('data-error');
$("#frmDedicatedOrShared").hide();
$("#frmDedicatedOrShared").removeAttr('required');
$("#frmDedicatedOrShared").removeAttr('data-error');
$("#frmServiceProvider").hide();
$("#frmServiceProvider").removeAttr('required');
$("#frmServiceProvider").removeAttr('data-error');
}
});
解决方案
尝试显示/隐藏父包装器
$('#frmProjectInterconnect').change(function(){
if($(this).val() == "Yes") {
$("#frmInterconnectionCapacity").parent().show();
$("#frmInterconnectionCapacity").attr('required', '');
$("#frmInterconnectionCapacity").attr('data-error', 'This field is required.');
$("#frmDedicatedOrShared").parent().show();
$("#frmDedicatedOrShared").attr('required', '');
$("#frmDedicatedOrShared").attr('data-error', 'This field is required.');
$("#frmServiceProvider").parent().show();
$("#frmServiceProvider").attr('required', '');
$("#frmServiceProvider").attr('data-error', 'This field is required.');
} else {
$("#frmInterconnectionCapacity").parent().hide();
$("#frmInterconnectionCapacity").removeAttr('required');
$("#frmInterconnectionCapacity").removeAttr('data-error');
$("#frmDedicatedOrShared").parent().hide();
$("#frmDedicatedOrShared").removeAttr('required');
$("#frmDedicatedOrShared").removeAttr('data-error');
$("#frmServiceProvider").parent().hide();
$("#frmServiceProvider").removeAttr('required');
$("#frmServiceProvider").removeAttr('data-error');
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css"></link>
<div class="form-floating mb-4">
<select class="form-select" id="frmProjectInterconnect" required>
<option value="" selected disabled></option>
<option>Yes</option>
<option>No</option>
</select>
<label for="frmProjectInterconnect">Project Interconnection Point</label>
</div>
<div class="form-floating mb-4">
<input type="number" class="form-control" id="frmInterconnectionCapacity" required>
<label for="frmInterconnectionCapacity">Interconnection Capacity (Mbps)</label>
</div>
<div class="form-floating mb-4">
<select class="form-select" id="frmDedicatedOrShared" required>
<option value="" selected disabled> </option>
<option>Dedicated</option>
<option>Shared</option>
</select>
<label for="frmDedicatedOrShared">Dedicated or Shared?</label>
</div>
<div class="form-floating mb-4">
<input type="text" class="form-control" id="frmServiceProvider" required>
<label for="frmServiceProvider">Service Provider</label>
</div>
编辑:对于案例下拉选项在最后,如果我们不隐藏父包装,看起来标签是隐藏的,但实际上它们就在下拉列表的后面。我添加了 1 秒超时来隐藏下拉菜单,并且标签将可见。请参阅代码片段。
$('#frmProjectInterconnect').change(function(){
if($(this).val() == "Yes") {
$("#frmInterconnectionCapacity").show();
$("#frmInterconnectionCapacity").attr('required', '');
$("#frmInterconnectionCapacity").attr('data-error', 'This field is required.');
$("#frmDedicatedOrShared").show();
$("#frmDedicatedOrShared").attr('required', '');
$("#frmDedicatedOrShared").attr('data-error', 'This field is required.');
$("#frmServiceProvider").show();
$("#frmServiceProvider").attr('required', '');
$("#frmServiceProvider").attr('data-error', 'This field is required.');
} else {
$("#frmInterconnectionCapacity").hide();
$("#frmInterconnectionCapacity").removeAttr('required');
$("#frmInterconnectionCapacity").removeAttr('data-error');
$("#frmDedicatedOrShared").hide();
$("#frmDedicatedOrShared").removeAttr('required');
$("#frmDedicatedOrShared").removeAttr('data-error');
$("#frmServiceProvider").hide();
$("#frmServiceProvider").removeAttr('required');
$("#frmServiceProvider").removeAttr('data-error');
setTimeout(() => {
$('#frmProjectInterconnect').hide();
}, 1000);
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css"></link>
<div class="form-floating mb-4">
<input type="number" class="form-control" id="frmInterconnectionCapacity" required>
<label for="frmInterconnectionCapacity">Interconnection Capacity (Mbps)</label>
</div>
<div class="form-floating mb-4">
<select class="form-select" id="frmDedicatedOrShared" required>
<option value="" selected disabled> </option>
<option>Dedicated</option>
<option>Shared</option>
</select>
<label for="frmDedicatedOrShared">Dedicated or Shared?</label>
</div>
<div class="form-floating mb-4">
<input type="text" class="form-control" id="frmServiceProvider" required>
<label for="frmServiceProvider">Service Provider</label>
</div>
<div class="form-floating mb-4">
<select class="form-select" id="frmProjectInterconnect" required>
<option value="" selected disabled></option>
<option>Yes</option>
<option>No</option>
</select>
<label for="frmProjectInterconnect">Project Interconnection Point</label>
</div>
推荐阅读
- python - 用坐标系求三角形的内角
- java - 将 2 个流转换为一个流和 map()
- python - 是否有将 gstreamer 管道转换为 python 代码的指南?
- java - 在 IBM Liberty 服务器的 Web 应用程序中解码 AES 加密
- r - 如何在不包含其他列的同名列的情况下使用数据框子集?
- javascript - 从本地存储 Angular 8 获取对象
- z3 - Z3py:将值添加到计算结果并进行另一次检查
- sql - 使第一个“NOT_Ready”和“LOGOUT”状态之后的所有其他“NOT_READY”状态为“LOGIN”
- python - 如何记住极简编码的语法?
- php - 在php中检查字符串中的单词数组