remove the repeated values

Asked

Viewed 2,437 times

0

If it type 1, I have to check that in the table there is no value 1 that has already been typed, if there is the repeated value, remove the value 1 and keep the other 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’m not finding a solution, could help me?

  • What repeated values do you want to remove? It could detail the question better?

  • If it type 1, I have to check that in the table there is no value 1 that has already been typed, if there is a repeated value, remove the value 1 and keep the other one intact. so for other values.

  • is a repeat list for each field or for all Fields?

  • all text inputs are empty, if I type 1 I must check in all text inputs of the table if there is this value, if there is one, I must remove this last repeated value and keep the other.

2 answers

1


In this part here you are adding in the array the current value and then traverse the even array searching for values repeated, that way he ALWAYS will find 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 flip:

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 entering the same value in the same field, simply create an onfocus that will save the original value of the field before it changes. Do so:

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

And in his method of onchange, need to remove its old value:

var index = array.indexOf(valorAnterior);
if (index > -1) {
    array.splice(index, 1); // remove
}
// ... procura repetido 
  • It worked, but there’s a problem: if I delete the text input that is already filled (say the input has value=1) and take it out of focus, it adds the value in the array. if I delete (value=1) in this same input that value I just filled and put that same value (value=1) erased in another input position, it will say that the value is repeated.

  • @Erry I updated my answer with a hint of this problem ;)

  • 1

    thanks, it worked. just one more addendo, just put inside the event onfocus the condition: if($(this).val() !=""){valueAnterior = $(this). val();}

1

It is no more practical to use the index method than to use a FOR?

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

There is an example working on this link here: example

Browser other questions tagged

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