0
I am trying to do a validation so that only be allowed to record the records if all radios
are marked, or to Sim
or to Não
, I’m creating the radios
dynamically but the code I did to try this validation is not working, as can be seen below.
The button that triggers the action is like this:
<button type="submit" class="btn btn-primary" id="Finaliza">
Gravar
</button>
The script I made:
document.getElementById("Finaliza").onclick = function() {
var radios = document.getElementsByName("Status[]");
for (var i = 0; i < radios.length; i++) {
if (radios[i].unchecked) {
alert("Você precisa selecionar o status");
}
}
};
The creation of radios are like this:
<p>
<label class="radio-inline">
<input type="radio" name="Status[]<?php echo $IdItemCheckList; ?>" value="1"> Sim
</label>
<label class="radio-inline">
<input type="radio" name="Status[]<?php echo $IdItemCheckList; ?>" value="0"> Não
</label>
</p>
I think the reason is
getElementsByName
. You are using.getElementsByName("Status[]")
, as long as theinputs
are named afterStatus[]<?php echo $IdItemCheckList; ?>
, that is to say,Status[]Qualquercoisa
is different fromStatus[]
.– Inkeliz