3
I have a problem filling a field within a table that is within a loop For
, and one of the titles of this table is a button modal,
that as it is clicked opens a selection of units of measure. And to the user select the unit, the field input
is filled, but as defined an array it fills the whole table. As in the following picture. Have some way to fill only the selected input, or, just give a focus
in one place of all ?
<script type="text/javascript">
$(function(){
var prevFocus; //Escopo global
//Ao selecionar a unidade de medida o último input focado será preenchido!
$("#unidadeMedida").on("change", function(){
if (typeof prevFocus !== "undefined") {
prevFocus.val($(this).val());
}
});
$(".inputTabela").focus(function() {
prevFocus = $(this);
});
});
</script>
<div class="modal fade" id="myModaler" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<label class="modal-title">Cadastro de Unidades de Medida</label>
</div>
<div class="modal-body">
<table class="display example" cellspacing="0" width="100%">
<thead>
<tr>
<th> Unidade </th>
<th> Descrição </th>
</tr>
</thead>
<tbody>
<?php
include ("conn.php");
$result = "SELECT * FROM cadunid ORDER BY descricao";
$resultado = mysqli_query($conn, $result);
while($row = mysqli_fetch_assoc($resultado)) {
echo "<tr class='btn-default id='unidadeMedida'>";
echo "<td class='get-value'>". $row['codigo'] ."</td>";
echo "<td class='get-value-codigo'>". $row['descricao'] ."</td>";
echo "</tr>";
}
?>
</tbody>
</table>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Fechar</button>
</div>
</div>
</div>
</div>
<table class="table table-striped table-bordered table-hover">
<thead>
<tr>
<th>
<button type="button" id='unidadeMedida' class="" data-toggle="modal" data-target="#myModaler">Unidade >></button>
</th>
<th>Preço Custo</th>
<th>Preço Venda</th>
<th>Peso Bruto</th>
<th>Peso Liquido</th>
<th>Qtd. Emb.</th>
</tr>
</thead>
<tbody id='prprod'><!-- Início do Corpo da tabela / Quantidade de linhas e colunas -->
<?php for($i = 0; $i <= 5; $i++){ // loop para criar 5 linhas na tabela ?>
<tr>
<input type="hidden" maxlength="6" name="recnum[]" style="border:none; width:100%; background-color: transparent;">
<td><input type='text' class='inputTabela' name="unidad[]" style="border:none; width:100%; background-color: transparent;">
<td><input type="text" class='' maxlength="" name="preco_custo[]" placeholder="" style="border:none; width:100%; background-color: transparent;"></td>
<td><input type="text" class='' maxlength="" name="preco_venda[]" placeholder="" style="border:none; width:100%; background-color: transparent;"></td>
<td><input type="text" class='' maxlength="" name="peso_bruto[]" placeholder="" style="border:none; width:100%; background-color: transparent;"></td>
<td><input type="text" class='' maxlength="" name="peso_liquido[]" placeholder="" style="border:none; width:100%; background-color: transparent;"></td>
<td><input type="text" class='' maxlength="" name="qtde_embalagem[]" placeholder="" style="border:none; width:100%; background-color: transparent;"></td>
</tr>
<?php } ?>
</tbody><!-- Fim do Corpo da tabela / Quantidade de linhas e colunas -->
</table>
so, I did a test here on stackoverflow, and if I put the script tag and the Js inside it doesn’t run at all
– Victor
So the stackoverflow simulator already makes some things easy to stop when we answer, but you will need to review some concepts of web applications, mainly to be able to debug and identify the error.
– Caique Romero
I no longer know what I do, because I do not understand how it does not work at all, and nor this example of yours helped, I never went through this kk
– Victor