I need the user to add the same fields as many times as he wants, and also to remove them. I was able to do the 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 he does
When I click the adicionar
button it creates all 3 fields within a div .conteudoIndividual
, I created a hidden
field to control that amount of lines.