Error when incrementing value in inputs with same class?

2

I have a function that creates an input loop depending on the number of parcels inserted by the user and when entering a value in the first it increases by 1 each of the following inputs, so far so good, but if you have entered 10 payments and From the fifth installment he has given a check sequence of another bead that is not sequential to the previous bead, I need to insert the check number by +1 in the following example:

  

bead 1 checks starts on sheet 2034 and ends on sheet 2038

parcela 1: 2034
parcela 2: 2035
parcela 3: 2036
parcela 4: 2037
parcela 5: 2038
  

check 2 that starts with sheet number 7332

parcela 6: 7332
parcela 7: 7333
parcela 8: 7334
parcela 9: 7335
parcela 10: 7336

But what is actually happening is when you enter a new value in position 5 it increments wrongly, and the blur also interferes with the values even if you have not changed it. What workaround is missing, or should I change the entire structure?

$(document).on('blur', '.ncheque', function() {
  var chqs = $('.ncheque');
  var index = null;
  var partes = this.value.split('-');
  var valor = parseInt(partes[1] || partes[0]);
  indice = chqs.index(this);
  if (valor) {
    $('.ncheque').each(function(index) {
      if (index > indice) {
        $(this).val([partes[1] ? partes[0] : null, valor + index].filter(Boolean).join('-'));
      }
    });
  } else {
    $('.ncheque').val('');
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><inputtype="text" class="ncheque"><br>
<input type="text" class="ncheque"><br>
<input type="text" class="ncheque"><br>
<input type="text" class="ncheque"><br>
<input type="text" class="ncheque"><br>
<input type="text" class="ncheque"><br>
<input type="text" class="ncheque"><br>
    
asked by anonymous 04.07.2018 / 21:21

1 answer

1

A simpler way to do this compared to your code. When you change the value of a input , only the following will be modified by incrementing +1 to input changed.

$(document).on('blur', '.ncheque', function() {
   var chqs = $('.ncheque');
   var qtd = chqs.length; // quantidade de inputs
   var indice = chqs.index(this); // pega o índice do input blur
   for(var x=indice; x<qtd; x++){
      chqs[x+1] ? chqs[x+1].value = parseInt(chqs[x].value)+1 : null; // incrementa os seguintes até o final
   }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><inputtype="text" class="ncheque"><br>
<input type="text" class="ncheque"><br>
<input type="text" class="ncheque"><br>
<input type="text" class="ncheque"><br>
<input type="text" class="ncheque"><br>
<input type="text" class="ncheque"><br>
<input type="text" class="ncheque"><br>
    
05.07.2018 / 01:53