3
I need the user to add the same fields as many times as he wants, and also remove them. I was able to script to add the fields, but remove is not working.
HTML
<a href="#" data-id="1" id="adicionarCampo">+ adicionar campo</a>
<form method="POST" action="gerarpdf.php" target="_blank">
<div id="formulario">
<input type="text" placeholder="Nº do Documento" maxlength="6" name="numeroDocumento" required/>
<select name="tipoDocumento" required>
<option value="" disabled selected>Tipo do Documento</option>
<option value="01">Volvo</option>
<option value="02">Saab</option>
</select>
<select name="subTipoDocumento" required>
<option value="" disabled selected>Subtipo do Documento</option>
<option value="01">Volvo</option>
<option value="02">Saab</option>
</select>
</div>
<input type="submit" value="Gerar Código"/>
</form>
Javascript
$(function() {
var divContent = $('#formulario');
var botaoAdicionar = $('a[data-id="1"]');
var i = 1;
//Ao clicar em adicionar ele cria uma linha com novos campos
$(botaoAdicionar).click(function() {
$('<div class="conteudoIndividual"><input type="text" placeholder="Nº do Documento" maxlength="6" name="numeroDocumento'+i+'" required/><select name="tipoDocumento'+i+'" required><option value="" disabled selected>Tipo do Documento</option><option value="01">Volvo</option><option value="02">Saab</option></select><select name="subTipoDocumento'+i+'" required><option value="" disabled selected>Subtipo do Documento</option><option value="01">Volvo</option><option value="02">Saab</option></select><a href="#" id="linkRemover">- Remover Campos</a></div>').appendTo(divContent);
$('#removehidden').remove();
i++;
$('<input type="hidden" name="quantidadeCampos" value="'+i+'" id="removehidden">').appendTo(divContent);
});
//Cliquando em remover a linha é eliminada
$('#linkRemover').live('click', function() {
$(this).parents('.conteudoIndividual').remove();
i--;
});
});
What does he do
When I click on the adicionar
it creates all 3 fields within a div .conteudoIndividual
, created a field hidden
to control that amount of lines.
@Sergio Bom, this I have already changed to class. Well, but I understand, choose this as an answer and have something wrong like this.
– Allan Ramos
@Tuyoshivinicius also thank you for your help :).
– Allan Ramos
+1
so have another version of how to solve the problem.– Sergio