gpt4 book ai didi

javascript - 使用 Vanilla Javascript 切换复选框显示/隐藏两个 DIV 元素

转载 作者:行者123 更新时间:2023-12-02 21:45:10 25 4
gpt4 key购买 nike

我有两个表单组,都位于 DIV 元素内。第一个有一个 id="personaFisicaFormGroup"

第二个有一个 id="personaMoralFormGroup"

personaFisicaFormGroup 默认为 display: block;

personaMoralFormGroup 默认为 display: none;

当用户单击复选框时,我想反转显示。

personaFisicaFormGroup 设置为 display: none;

personaMoralFormGroup 设置为 display: blcok;

如果用户再次点击,我想切换值blocknone(不确定次数)。

编辑:我刚刚将我的 Javascript 更改为:

onOffSwitch.addEventListener('change', e => { 
if(personaMoralFormGroup.style.display == 'none') {
personaMoralFormGroup.style.display = 'block';
personaFisicaFormGroup.style.display = 'none';
} else {
personaMoralFormGroup.style.display = 'none';
personaFisicaFormGroup.style.display = 'block';
}
});

这可行,但在第二次开关单击时。

这是我的代码:

const onOffSwitch = document.getElementById('onOffSwitch');
const personaFisicaFormGroup = document.getElementById('personaFisicaFormGroup');
const personaMoralFormGroup = document.getElementById('personaMoralFormGroup');


onOffSwitch.addEventListener('change', e => {
if(e.target.checked){
personaMoralFormGroup.style.display = 'block';
} else {
personaFisicaFormGroup.style.display = 'none';
}
});
.nuevo-ingreso-form__group {
border: 1px solid var(--clr-grey-borders);
border-radius: 5px;
margin-bottom: 20px;
background-color: rgb(255, 255, 255);
width: 300px;
}
#personaMoralFormGroup {
display: none;
}
.nuevo-ingreso-form__group:hover {
box-shadow: 0 7px 16px 0 rgba(0,0,0,.2), 0 1px 3px 0 rgba(0,0,0,.1);
transition: box-shadow .3s ease-out,-webkit-box-shadow .3s ease-out;
}

.nuevo-ingreso-form__group-header {
padding: 25px 40px 10px;
display: flex;
align-items: center;
font-size: 18px;
}
.nuevo-ingreso-form__group-header h3 {
margin-left: 20px;
}
.nuevo-ingreso-form__field-wrapper {
padding: 0px 40px;
padding-top: 14px;
display: flex;
justify-content: space-between;
align-items: center;
}
.nuevo-ingreso-form__field-wrapper:last-child {
padding-bottom: 50px;

}
.nuevo-ingreso-form__input-label {
font-size: 13px;
font-weight: 800;
color: grey;
padding: 5px 5px 5px 0;
display: block;
width: 40%;
text-align: right;
}
.nuevo-ingreso-form__input-container {
border: 1px solid grey;
height: 100%;
border-radius: 5px;
flex-grow: 1;
margin-left: 20px;
}
.nuevo-ingreso-form__input-field {
margin: 0;
background-color: transparent;
border: none;
width: 100%;
font-size: 16px;
}

.onoffswitch {
position: relative; width: 172px;
-webkit-user-select:none; -moz-user-select:none; -ms-user-select: none;
margin: 0 auto;
}
.onoffswitch-checkbox {
display: none;
}
.onoffswitch-label {
display: block; overflow: hidden; cursor: pointer;
border: 2px solid #FFFFFF; border-radius: 50px;
}
.onoffswitch-inner {
display: block; width: 200%; margin-left: -100%;
transition: margin 0.3s ease-in 0s;
}
.onoffswitch-inner:before, .onoffswitch-inner:after {
display: block; float: left; width: 50%; height: 35px; padding: 0; line-height: 35px;
font-size: 15px; color: white; font-family: Trebuchet, Arial, sans-serif; font-weight: bold;
box-sizing: border-box;
}
.onoffswitch-inner:before {
content: "Persona Física";
padding-left: 19px;
background-color: #E0E0E0; color: #5C5C5C;
}
.onoffswitch-inner:after {
content: "Persona Moral";
padding-right: 19px;
background-color: #636363; color: #FFFFFF;
text-align: right;
}
.onoffswitch-switch {
display: block; width: 18px; margin: 8.5px;
background: #FFFFFF;
position: absolute; top: 0; bottom: 0;
right: 133px;
border: 2px solid #FFFFFF; border-radius: 50px;
transition: all 0.3s ease-in 0s;
}
.onoffswitch-checkbox:checked + .onoffswitch-label .onoffswitch-inner {
margin-left: 0;
}
.onoffswitch-checkbox:checked + .onoffswitch-label .onoffswitch-switch {
right: 0px;
}
<div class="onoffswitch">
<input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="onOffSwitch">
<label class="onoffswitch-label" for="onOffSwitch">
<span class="onoffswitch-inner"></span>
<span class="onoffswitch-switch"></span>
</label>
</div>

<!-- DATOS GENERALES - PERSONA FISICA -->
<div id="personaFisicaFormGroup" class="nuevo-ingreso-form__group">
<div class="nuevo-ingreso-form__group-header">
<h3>Persona Fisica</h3>
</div>
<div class="nuevo-ingreso-form__field-wrapper">
<label class="nuevo-ingreso-form__input-label" for="">NOMBRE</label>
<div class="nuevo-ingreso-form__input-container">
<input id="clienteNombreORazonSocial" class="nuevo-ingreso-form__input-field" type="text">
</div>
</div>
<div class="nuevo-ingreso-form__field-wrapper">
<label class="nuevo-ingreso-form__input-label" for="">APELLIDO PATERNO</label>
<div class="nuevo-ingreso-form__input-container">
<input id="clientePaterno" class="nuevo-ingreso-form__input-field" type="text">
</div>
</div>
<div class="nuevo-ingreso-form__field-wrapper">
<label class="nuevo-ingreso-form__input-label" for="">APELLIDO MATERNO</label>
<div class="nuevo-ingreso-form__input-container">
<input id="clienteMaterno" class="nuevo-ingreso-form__input-field" type="text">
</div>
</div>
</div>

<!-- PERSONA MORAL -->
<div id="personaMoralFormGroup" class="nuevo-ingreso-form__group">
<div class="nuevo-ingreso-form__group-header">
<i class="fa fa-id-card"></i>
<h3>Persona Moral</h3>
</div>
<div class="nuevo-ingreso-form__field-wrapper">
<label class="nuevo-ingreso-form__input-label" for="">RAZÓN SOCIAL</label>
<div class="nuevo-ingreso-form__input-container">
<input id="clienteNombreORazonSocial" class="nuevo-ingreso-form__input-field" type="text">
</div>
</div>
<div class="nuevo-ingreso-form__field-wrapper">
<label class="nuevo-ingreso-form__input-label" for="">RFC</label>
<div class="nuevo-ingreso-form__input-container">
<input id="clienteRFC" class="nuevo-ingreso-form__input-field" type="text">
</div>
</div>
<div class="nuevo-ingreso-form__field-wrapper">
<label class="nuevo-ingreso-form__input-label" for="">EMAIL</label>
<div class="nuevo-ingreso-form__input-container">
<input id="clienteEmail" class="nuevo-ingreso-form__input-field" type="text">
</div>
</div>
</div>

最佳答案

每当隐藏和显示元素时,我更喜欢为此使用显式类。添加和删​​除类更容易,而且我不必担心元素默认情况下是否不是 display:block

这将为您带来以下结果。需要注意的是:我已经根据 Id 删除了 display:none ,我们正在使用 classList.toggle添加和删​​除类

const onOffSwitch = document.getElementById('onOffSwitch');
const personaFisicaFormGroup = document.getElementById('personaFisicaFormGroup');
const personaMoralFormGroup = document.getElementById('personaMoralFormGroup');


onOffSwitch.addEventListener('change', e => {
personaFisicaFormGroup.classList.toggle("hidden", e.target.checked);
personaMoralFormGroup.classList.toggle("hidden", !e.target.checked);
});
.hidden {display:none;}

.nuevo-ingreso-form__group {
border: 1px solid var(--clr-grey-borders);
border-radius: 5px;
margin-bottom: 20px;
background-color: rgb(255, 255, 255);
width: 300px;
}

.nuevo-ingreso-form__group:hover {
box-shadow: 0 7px 16px 0 rgba(0,0,0,.2), 0 1px 3px 0 rgba(0,0,0,.1);
transition: box-shadow .3s ease-out,-webkit-box-shadow .3s ease-out;
}

.nuevo-ingreso-form__group-header {
padding: 25px 40px 10px;
display: flex;
align-items: center;
font-size: 18px;
}
.nuevo-ingreso-form__group-header h3 {
margin-left: 20px;
}
.nuevo-ingreso-form__field-wrapper {
padding: 0px 40px;
padding-top: 14px;
display: flex;
justify-content: space-between;
align-items: center;
}
.nuevo-ingreso-form__field-wrapper:last-child {
padding-bottom: 50px;

}
.nuevo-ingreso-form__input-label {
font-size: 13px;
font-weight: 800;
color: grey;
padding: 5px 5px 5px 0;
display: block;
width: 40%;
text-align: right;
}
.nuevo-ingreso-form__input-container {
border: 1px solid grey;
height: 100%;
border-radius: 5px;
flex-grow: 1;
margin-left: 20px;
}
.nuevo-ingreso-form__input-field {
margin: 0;
background-color: transparent;
border: none;
width: 100%;
font-size: 16px;
}

.onoffswitch {
position: relative; width: 172px;
-webkit-user-select:none; -moz-user-select:none; -ms-user-select: none;
margin: 0 auto;
}
.onoffswitch-checkbox {
display: none;
}
.onoffswitch-label {
display: block; overflow: hidden; cursor: pointer;
border: 2px solid #FFFFFF; border-radius: 50px;
}
.onoffswitch-inner {
display: block; width: 200%; margin-left: -100%;
transition: margin 0.3s ease-in 0s;
}
.onoffswitch-inner:before, .onoffswitch-inner:after {
display: block; float: left; width: 50%; height: 35px; padding: 0; line-height: 35px;
font-size: 15px; color: white; font-family: Trebuchet, Arial, sans-serif; font-weight: bold;
box-sizing: border-box;
}
.onoffswitch-inner:before {
content: "Persona Física";
padding-left: 19px;
background-color: #E0E0E0; color: #5C5C5C;
}
.onoffswitch-inner:after {
content: "Persona Moral";
padding-right: 19px;
background-color: #636363; color: #FFFFFF;
text-align: right;
}
.onoffswitch-switch {
display: block; width: 18px; margin: 8.5px;
background: #FFFFFF;
position: absolute; top: 0; bottom: 0;
right: 133px;
border: 2px solid #FFFFFF; border-radius: 50px;
transition: all 0.3s ease-in 0s;
}
.onoffswitch-checkbox:checked + .onoffswitch-label .onoffswitch-inner {
margin-left: 0;
}
.onoffswitch-checkbox:checked + .onoffswitch-label .onoffswitch-switch {
right: 0px;
}
<div class="onoffswitch">
<input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="onOffSwitch">
<label class="onoffswitch-label" for="onOffSwitch">
<span class="onoffswitch-inner"></span>
<span class="onoffswitch-switch"></span>
</label>
</div>

<!-- DATOS GENERALES - PERSONA FISICA -->
<div id="personaFisicaFormGroup" class="nuevo-ingreso-form__group">
<div class="nuevo-ingreso-form__group-header">
<h3>Persona Fisica</h3>
</div>
<div class="nuevo-ingreso-form__field-wrapper">
<label class="nuevo-ingreso-form__input-label" for="">NOMBRE</label>
<div class="nuevo-ingreso-form__input-container">
<input id="clienteNombreORazonSocial" class="nuevo-ingreso-form__input-field" type="text">
</div>
</div>
<div class="nuevo-ingreso-form__field-wrapper">
<label class="nuevo-ingreso-form__input-label" for="">APELLIDO PATERNO</label>
<div class="nuevo-ingreso-form__input-container">
<input id="clientePaterno" class="nuevo-ingreso-form__input-field" type="text">
</div>
</div>
<div class="nuevo-ingreso-form__field-wrapper">
<label class="nuevo-ingreso-form__input-label" for="">APELLIDO MATERNO</label>
<div class="nuevo-ingreso-form__input-container">
<input id="clienteMaterno" class="nuevo-ingreso-form__input-field" type="text">
</div>
</div>
</div>

<!-- PERSONA MORAL -->
<div id="personaMoralFormGroup" class="nuevo-ingreso-form__group hidden">
<div class="nuevo-ingreso-form__group-header">
<i class="fa fa-id-card"></i>
<h3>Persona Moral</h3>
</div>
<div class="nuevo-ingreso-form__field-wrapper">
<label class="nuevo-ingreso-form__input-label" for="">RAZÓN SOCIAL</label>
<div class="nuevo-ingreso-form__input-container">
<input id="clienteNombreORazonSocial" class="nuevo-ingreso-form__input-field" type="text">
</div>
</div>
<div class="nuevo-ingreso-form__field-wrapper">
<label class="nuevo-ingreso-form__input-label" for="">RFC</label>
<div class="nuevo-ingreso-form__input-container">
<input id="clienteRFC" class="nuevo-ingreso-form__input-field" type="text">
</div>
</div>
<div class="nuevo-ingreso-form__field-wrapper">
<label class="nuevo-ingreso-form__input-label" for="">EMAIL</label>
<div class="nuevo-ingreso-form__input-container">
<input id="clienteEmail" class="nuevo-ingreso-form__input-field" type="text">
</div>
</div>
</div>

关于javascript - 使用 Vanilla Javascript 切换复选框显示/隐藏两个 DIV 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60270168/

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