Bug in Javascript validation

Asked

Viewed 28 times

0

Hello. I have developed a process bar with a next and preview button, where clicking next will reduce the processes.

I have 2 fields on the screen, "position" and "totalItems", where position needs to be less than or equal to totalItems.

My problem is that my bar has 3 sessions and a total of items, when the first section ends the counter understands that the bar is over and does not continue to count the positions, stating that the document is in the first record.

Thus...

The first session has 9 items, so the button goes from 0 to 8, where 8 > 78 = false. But the next position, which would be 9 the comparison 9 > 78 = true and features the alert.

Placing a console.log I see that the result of the fields are correct, one with value 8 and the other with value 78, which is the total of items.

bindEvent("click",".next",function(event){
    event.preventDefault();
    var indicator           = $("input[name='indicator']").val(),
        position            = $("input[name='position']").val(),
        totalItens          = "<cfoutput>#arrayLen(session.Propostas)#</cfoutput>"; //Pega o total de itens em uma sessão do CF = 78
    if(position >= totalItens){
        alert("Você está no último registro");
    }
    else{
        //increment +1
        position++;
        $("input[name='position']").val(position);
        //Tira 1 do total de itens
        $("input[name='indicator']").val(("<cfoutput>#arrayLen(session.Propostas)#</cfoutput>")-position);
        .
        .
        .
    }
}

Does anyone have any idea what might be going on?

  • Well I ended up identifying the problem, for some reason js is treating the contents of my variables as string.

1 answer

0

Oops, I just identified my problem.

js is treating the contents of the fields as strings, so I used the js number and it worked.

Stayed like this...

bindEvent("click",".next",function(event){
    event.preventDefault();
    var indicator           = $("input[name='indicator']").val(),
        position            = $("input[name='position']").val(),
        totalItens          = "<cfoutput>#arrayLen(session.Propostas)#</cfoutput>"; //Pega o total de itens em uma sessão do CF = 78
    if(Number(position) >= Number(totalItens)){
        alert("Você está no último registro");
    }
    else{
        //increment +1
        position++;
        $("input[name='position']").val(position);
        //Tira 1 do total de itens
        $("input[name='indicator']").val(("<cfoutput>#arrayLen(session.Propostas)#</cfoutput>")-position);
        .
        .
        .
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.