remove repeated values

0

If he types 1, I have to check if there is no value 1 in the table that has already been typed, if there is a value repeated, it removes the value 1 and keeps the other one intact. So for other values.

HTML

        <table>
            <tr>
                 <td><input type="text" name="notapergunta2_1" placeholder="Alfa"/> Alfa</td>
                 <td><input type="text" name="notapergunta2_2" placeholder="Chub"/> Chub</td>
                 <td><input type="text" name="notapergunta2_3" placeholder="Met "/> Met </td>
             </tr>
             <tr>
                 <td><input type="text" name="notapergunta2_4" placeholder="A"/> A</td>
                 <td><input type="text" name="notapergunta2_5" placeholder="G"/> G</td>
                 <td><input type="text" name="notapergunta2_6" placeholder="N"/> N</td>
             </tr>
             <tr>
                 <td><input type="text" name="notapergunta2_7" placeholder="ASS"/> ASS</td>
                 <td><input type="text" name="notapergunta2_8" placeholder="Hdi"/> Hdi</td>
                 <td><input type="text" name="notapergunta2_9" placeholder="Porto"/> Porto </td>
             </tr>
        </table>

My javascript:

array = []
for(var i = 0; i<9;i++){
    var numeroDaPergunta = i+1;

    $("[name='notapergunta2_"+numeroDaPergunta+"']").on("change",function(){
        var verificaValorDigitado = $(this).val();

        if(verificaValorDigitado > 3 || verificaValorDigitado <= 0){
            alert("Valor Digitado : "+verificaValorDigitado+"\nDigite Números de 1 a 3");
            $(this).val("");
        } else {
            array.push($(this).val());
            for(var j = 0; j<array.length; j++){
                if(verificaValorDigitado == array[j]){
                    alert("Valor Repetido");
                    $(this).val("");
                    return false;
                }
            }
        }
    });
}

I can not find a solution, could you help me?

    
asked by anonymous 23.06.2015 / 17:40

2 answers

1

In this part here you are adding the current value in the array and then scrolling through the same array looking for repeated values, so it ALWAYS will find out why you are adding BEFORE .

array.push($(this).val());
for(var j = 0; j<array.length; j++){
    if(verificaValorDigitado == array[j]){
        alert("Valor Repetido");
        $(this).val("");
        return false;
    }
}

Just reverse:

for(var j = 0; j<array.length; j++){
    if(verificaValorDigitado == array[j]){
        alert("Valor Repetido");
        $(this).val("");
        return false;
    }
}
array.push($(this).val());


To avoid the problem of typing the same value in the same field, simply create a onfocus event that will save the original value of the field before it changed. Here's how:

var valorAnterior; /* variavel global */
$("[name='notapergunta2_"+numeroDaPergunta+"']").on("focus",function(){
    valorAnterior = $(this).val();
}

And in your onchange method, you need to remove its old value:

var index = array.indexOf(valorAnterior);
if (index > -1) {
    array.splice(index, 1); // remove
}
// ... procura repetido 
    
23.06.2015 / 18:03
1

Is not it more practical to use the indexOf method than to traverse a FOR?

var valor = document.querySelector('ELEMENTO_A_SER_CAPTURADO').value;
if (array.indexOf(valor) < 0) 
    array.push(valor);
else 
    alert('valor duplicado');

You have an example working at this link here: example

    
23.06.2015 / 18:15