3
I have a group of Radio Button, some of them have other inputs as children, if you check the Input Pai they must appear, if it is unchecked they must disappear.
As in the HTML down below:
<div style="padding-left:30px;clear:both;" class="linha-opcao-resposta">
<div style="float:left;">
<label for="7329_2338" title="">27.3 Presença de agentes específicos na citologia oncótica?</label>
<input type="hidden" title="" id="7329_2338" name="hidden_2338">
</div>
<div style="padding-left:30px;clear:both;" class="linha-opcao-resposta">
<div style="float:left;">
<input type="radio" title="" id="7330_2370" name="radio_2370" value="0">
<label for="7330_2370" title="">Não</label>
</div>
</div>
<div style="padding-left:30px;clear:both;" class="linha-opcao-resposta">
<div style="float:left;">
<input type="radio" title="" id="7331_2370" name="radio_2370" value="1">
<label for="7331_2370" title="">Sim </label>
</div>
<div style="padding-left: 30px; clear: both; display:none" class="linha-opcao-resposta">
<div style="float:left;">
<label for="7331_2371" title="">27.3.1 Agente 1</label>
<input type="text" title="" id="7331_2371" name="text_2371" class="rfdRoundedCorners rfdDecorated rfdInputDisabled">
</div>
</div>
<div style="padding-left: 30px; clear: both; display:none" class="linha-opcao-resposta">
<div style="float:left;">
<label for="7332_2371" title="">27.3.2 Agente 2</label>
<input type="text" title="" id="7332_2371" name="text_2371" class="rfdRoundedCorners rfdDecorated rfdInputDisabled">
</div>
</div>
<div style="padding-left: 30px; clear: both;display:none " class="linha-opcao-resposta">
<div style="float:left;">
<label for="7333_2371" title="">27.3.3 Agente 3</label>
<input type="text" title="" id="7333_2371" name="text_2371" class="rfdRoundedCorners rfdDecorated rfdInputDisabled">
</div>
</div>
</div>
<div style="padding-left:30px;clear:both;" class="linha-opcao-resposta">
<div style="float:left;">
<input type="radio" title="" id="7334_2370" name="radio_2370" value="2">
<label for="7334_2370" title="">Exame não realizado</label>
</div>
<div style="padding-left: 30px; clear: both; display:none" class="linha-opcao-resposta">
<div style="float:left;">
<label for="7331_2371" title="">27.3.1 Agente 1</label>
<input type="text" title="" id="7331_2371" name="text_2371" class="rfdRoundedCorners rfdDecorated rfdInputDisabled">
</div>
</div>
<div style="padding-left: 30px; clear: both; display:none" class="linha-opcao-resposta">
<div style="float:left;">
<label for="7332_2371" title="">27.3.2 Agente 2</label>
<input type="text" title="" id="7332_2371" name="text_2371" class="rfdRoundedCorners rfdDecorated rfdInputDisabled">
</div>
</div>
<div style="padding-left: 30px; clear: both;display:none " class="linha-opcao-resposta">
<div style="float:left;">
<label for="7333_2371" title="">27.3.3 Agente 3</label>
<input type="text" title="" id="7333_2371" name="text_2371" class="rfdRoundedCorners rfdDecorated rfdInputDisabled">
</div>
</div>
</div>
<div style="padding-left:30px;clear:both;" class="linha-opcao-resposta">
<div style="float:left;">
<input type="radio" title="" id="7405_2370" name="radio_2370" value="3">
<label for="7405_2370" title="">Não se aplica</label>
</div>
</div>
</div>
In the image above is correct, but if you select an input that also has children, you should close all others, as well as below.
The same should happen if you select a Radio Button that nay have children, close all child inputs group. As in the image below.
I made this code but I didn’t succeed...
The biggest problem is that it should be dynamic, should work on all radiobuttons, so I used the click event.
$('input[type="radio"]').on("change", function() {
var element = $(this);
var pais = element
.parent()
.parent()
.parent(); // SUBIR ATÉ A DIV SUPERIOR DO GRUPO
var filhos = pais.children(".linha-opcao-resposta"); // CAPTURAR AS DIVS FILHAS
var netos = element
.parent()
.parent()
.children(".linha-opcao-resposta"); // CAPTURAR OS INPUTS FILHOS DO RADIO BUTTON
if (netos.length > 0) {
for (let i = 0; i < netos.length; i++) {
netos[i].style.display = "block";
}
} else {
for (let i = 0; i < filhos.length; i++) {
netos = filhos[i].children();
alert(netos.length);
for (let j = 0; j < netos.length; j++) {
netos[j].children(".linha-opcao-resposta").style.display = "none";
}
}
}
});
I will start by recommending you to study CSS urgently. Styles inline are very bad for maintenance, besides leaving the polluted code.
– Woss
I am fully aware of the CSS but in the current situation of the code, I have no condition to apply CSS in this way... it is kind of necessary inline, this application is VB.NET and this code runs via Webbrowserform, hence it gets a little more complicated I would say.
– Carlito Murta