validating group of radios in javascript

1
function validarradios() {    
        var tab1 = $("#parte2").val(); --> vale 6 <br> 
        var list = new Array();

        for (var x = 0; x < tab1; x++) {
            var lsGrupo = "grupo3_" + x;
            var objGrupo = document.getElementsByName(lsGrupo);
            for (var i = 0; i < objGrupo.length; i++) {
                if (objGrupo[i].checked) {                    
                    if (i == 0) {
                        list.push("Bueno");
                    }
                if (i == 1) {
                    list.push("Malo");
                }
                if (i == 2) {
                    list.push("No Aplica");
                }
               }              
            }          
        }
    alert(list);          
    }

I do not know why when (x) is 6, the cycle is exited and the last position is not validated and I even put for (var x = 0; x <= tab1; x++) and nothing ... thanks in advance.

What happens is that the table that I have dynamics when he traveled to find the group of radios x and found a header where there was no group of radios only the initial question to answer others the cycle at the end did not validate the group of radios since it did not exist and increased the index at the end, what I did was create another validation in razor code to not increase the index when I found a header.

    
asked by Heiner Said 18.11.2016 в 23:43
source

1 answer

0

The loop for has the instruction not to reach 6. You can solve it in a couple of ways:

for (var x = 0; x < (tab1 + 1); x++) {

Or like this:

for (var x = 1; x < tab1; x++) {

Or like this:

for (var x = 0; x <= tab1; x++) {

On the other hand, use console.log() to know if it at least starts the loop.

for (var x = 1; x < tab1; x++) {
console.log(x);
    
answered by 19.11.2016 в 00:24