2
I know you have masks in jquery, for example jQuery Masked Input, but I am maintaining a system that was developed by another colleague and unfortunately is giving conflicts in the jquery that he implemented:
<script src="assets/js/jquery-3.1.1.min.js"></script>
To try to resolve the conflict, I tried to use the code below:
<script>var $jQuery = jQuery.noConflict();</script>
But unfortunately it didn’t work, because when I implement another jquery, some features stop working. I have the code below that provides this result:
<table border="0" width="100%">
<tr class='linhas'>
<td style="padding: 5px"><input type="text" name="NomePessoaAutorizada[]" class="form-control" placeholder="Nome completo" value=""></td>
<td style="padding: 5px; width: 8%"><input type="text" name="DDD[]" class="form-control" placeholder="DDD" value="" maxlength="2"></td>
<td style="padding: 5px"><input type="text" name="TelefoneCelularPessoaAutorizada[]" class="form-control pull-left" placeholder="Telefone celular" onkeypress="mascara(this, '#####-####')" maxlength="13"></td> <!-- data-inputmask="'alias': '(99)99999-9999'" -->
<td style="padding: 5px"><input type="text" name="RGPessoaAutorizada[]" class="form-control" placeholder="RG da pessoa autorizada" value=""></td>
<td style="padding: 5px"><button type="button" class="removerCampo btn btn-danger" title="Remover linha"><i class="fa fa-minus-square" aria-hidden="true"></i> Remover</button></td>
</tr>
<tr>
<td colspan="3"><button type="button" class="adicionarCampo btn btn-primary" title="Adicionar item"><i class="fa fa-plus-square" aria-hidden="true"></i> Adicionar mais contatos</button></td>
</tr>
</table>
<script type="text/javascript">
$(function () {
function removeCampo() {
$(".removerCampo").unbind("click");
$(".removerCampo").bind("click", function () {
if($("tr.linhas").length > 1){
$(this).parent().parent().remove();
}
});
}
$(".adicionarCampo").click(function () {
if ($('.linhas').length < 5) {
novoCampo = $("tr.linhas:first").clone();
novoCampo.find('input[type="text"]').val("");
novoCampo.find('select').val("");
novoCampo.insertAfter("tr.linhas:last");
removeCampo();
}
});
});
</script>
To avoid conflicts, I would like to know how to implement the mask in dynamic fields, but developed in Javascript and not in Jquery. Googling, I found this solution:
<script type="text/javascript">
function mascara(t, mask){
var i = t.value.length;
var saida = mask.substring(1,0);
var texto = mask.substring(i)
if (texto.substring(0,1) != saida){
t.value += texto.substring(0,1);
}
}
</script>
It works in parts because it presents 02 problems. The first is that it also accepts letters and the second is that, although maxlength is 13 characters long, it is not accepting the given value.
Perfect Netinho. It worked! Thank you very much!
– user24136