I have implemented the routine to verify if pressing a key in the INPUT field is typed only in digits. The first field works normally, but adding the second field no longer works. Does anyone know what the problem is?
$(document).ready(function () {
var bloco = $('[class^="bloco_"]');
var current_id = parseInt(bloco.attr("class").split("_")[1], 10);
$('input').bind('keypress', function (e) {
// if the letter is not digit then display error and don't type anything
if (e.which !== 8 && e.which !== 0 && (e.which < 48 || e.which > 57)) {
//display error message
$('.errmsg').css('color', 'red');
$('.errmsg').html('Digits Only').show().fadeOut('slow');
return false;
}
}).unbind('keypress').bind('keypress', function (e) {
// if the letter is not digit then display error and don't type anything
if (e.which !== 8 && e.which !== 0 && (e.which < 48 || e.which > 57)) {
//display error message
$('.errmsg').css('color', 'red');
$('.errmsg').html('Digits Only').show().fadeOut('slow');
return false;
}
});
$(document).on('click', '.btn-add', function (e) {
e.preventDefault();
var div_elements = $('form:first .elements'); // SELECIONA DIV ELEMENTS
var div_bloco = div_elements.find('.bloco_01:first'); // SELECIONA 1o BLOCO
var last_class_number = parseInt(div_elements.find('div[class^=bloco_]:last').attr("class").split("_")[1], 10); // PEGA NUM DO ULTIMO BLOCO
nextElement(div_bloco, last_class_number);
div_elements.find('div[class^=bloco_]:last span')
.removeClass('glyphicon-plus')
.addClass('glyphicon-minus')
.parent('button.btn-add')
.removeClass('btn-add')
.addClass('btn-del');
$('div[class^=bloco_]:last input').val('');
$(this).unbind('click').on('click', '.btn-add', function(e) {
div_elements.find('div[class^=bloco_]:last span')
.removeClass('glyphicon-plus')
.addClass('glyphicon-minus')
.parent('button.btn-add')
.removeClass('btn-add')
.addClass('btn-del');
$('div[class^=bloco_]:last input').val('');
});
}).on('click', '.btn-del', function (e) {
e.preventDefault();
var bloco = $(this).parent().parent().parent().parent().attr('class');
$('.' + bloco).remove();
$(this).unbind('click').on('click', '.btn-del', function(e) {
$('.' + bloco).remove();
});
});
function nextElement(element, current_id) {
var newElement = element.clone();
var id = current_id + 1;
current_id = id;
if (id < 10)
id = "0" + id;
var div_class_bloco = element.attr("class");
newElement.attr("class", div_class_bloco.split("_")[0] + "_" + id);
newElement.appendTo($(".elements"));
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script><linkhref="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<div class="col-sm-12">
<form id="certameForm" class="form-horizontal" method="POST" action="teste_etapa11.php">
<div class="elements">
<div class="bloco_01">
<!-- Secretaria -->
<div class="form-group">
<div class="col-sm-11 inputGroupContainer">
<label class="col-sm-2 control-label">Secretaria</label>
<div class="col-sm-9">
<select class="form-control" id="cd_secretaria[]" name="cd_secretaria[]">
<option value="0">--Selecione a Secretaria--</option>
<option value="1">Administracao</option>
<option value="2">Assuntos Juridicos</option>
<option value="3">Chefia de Gabinete do Prefeito</option>
<option value="4">Cidadania, Assistencia e Inclusao Social</option>
<option value="5">Comunicacao</option>
<option value="6">Cooperacao Internacional</option>
</select>
</div>
<div class="col-sm-1">
</div>
</div>
</div>
<div class="form-group">
<div class="col-sm-11 inputGroupContainer">
<label for="nu_qtd_vagas_por_secretaria[]" class="col-sm-2 control-label">Qtde Vagas por Secretaria</label>
<div class="col-sm-9">
<input type="text" class="form-control" id="nu_qtd_vagas_por_secretaria[]" name="nu_qtd_vagas_por_secretaria[]" placeholder="Quantidade de vagas por Secretaria" size="2" maxlength="2">
<span class="errmsg"></span>
</div>
</div>
</div>
<!-- plus -->
<div class="form-group">
<div class="col-sm-11 inputGroupContainer">
<div class="col-sm-offset-10 col-sm-1">
<button class="btn btn-primary btn-md btn-add">
<span class="glyphicon glyphicon-plus"></span>
</button>
</div>
</div>
</div>
</div>
</div>
<!-- Accept box and button submit -->
<div class="form-group">
<div class="col-sm-11 inputGroupContainer">
<div class="col-sm-offset-3 col-sm-8">
<ul class="list-unstyled pull-right">
<li><button type="submit" class="btn btn-primary next-step" id="submitted" name="submitted">Salvar e Continuar</button></li>
</ul>
</div>
</div>
</div>
</form>
</div>
</div>
Follow fiddle: link