2
I am generating a dynamic table through AJAX that in one of the TDS there is a link.
<a href="#modalInserirDescontoCB" class="modal-trigger id_aluno">104490</a>
The link must perform two tasks:
- Open a modal containing a form (class
modal-trigger
of materializecss is responsible); - Fill one of the form fields with the text of the clicked link.
The code of the second task:
$('.id_aluno').click(function () {
$('#campo_aluno').val(this.text);
});
Where #campo_aluno
is a input
modal.
AJAX that makes the request:
tabela = $('#tabela-resultado-alunos');
filtro = $('#aluno').val();
if (filtro == '') {
alert('Preencha o campo.');
return false;
}
$.ajax({
url: path + 'cb/lista-alunos',
type: 'post',
data: {
get: filtro,
},
dataType: 'html',
beforeSend: function () {
tabela.html('Carregando...');
},
success: function (retorno) {
tabela.remove('.bloco');
// Atribui o retorno HTML para a div correspondente
if (retorno == 0) {
$(tabela).html('Deu ruim');
} else {
$(tabela).html(retorno);
}
},
error: function (erro, er) {
$(tabela).html('Erro ' + erro.status + ' - ' + erro.statusText + '</br> Tipo: ' + er + '</p>');
}
});
The PHP function that returns the HTML table:
$output = <<<HTML
<table class="striped bordered">
<thead>
<tr>
<th>ID</th>
<th>NOME</th>
<th>CPF</th>
</tr>
</thead>
<tbody>
HTML;
foreach ($alunos as $a) {
$output .= <<<HTML
<tr>
<td><a href="#modalInserirDescontoCB" style="cursor: pointer" title="Preencher número de aluno com esse valor" class="modal-trigger id_aluno">{$a->pf_id}</a></td>
<td>{$a->nome}</td>
<td>{$a->cpf}</td>
</tr>
HTML;
}
$output .= '</tr></tbody></table>';
return $output;
When I click on one of the generated links, none of the functions are executed.
I also tried a simple $('.id_aluno').click(function(){console.log('funcionou')});
and nothing happens, as if the content generated was not seen by jquery.
Put the generation code snippet of these tags, please.
– BrTkCa
I shall supplement the question.
– Cobra