Get value from an array

2

I'm trying to get the value of a field from a table created with Ajax. With the function below, all fields with the name qtde appear, including what I need. I need to make a simple account of a typed field less this created ( qtde - qtde_trans ), but it's complicated.

Function that takes all fields with the name qtde:

$("input[name^='qtde']").each(function() {
    console.log($(this).val());
});

Return (the value I want, in the line of calculation, is 2000):

EntirefunctionthatI'mtesting(withthis.valIgetthetypefieldqtde_trans):

functioncalc_dif(){if($(this).val().length>0){vartotal=$(this).val();$("input[name^='qtde']").each(function() {
         console.log($(this).val());
       });
       if ($("#qtde").val() < $("#qtde_trans").val()) {
          alert("Menor");
       }else{
       alert(total2);
       }
  }
}

$(function() {
  var $table = $('#locais');
  $table.on('change', '.input', calc_dif);
  $("#qtde_trans").on('change', calc_dif);
});

How the table is created:

 for(var i = 0;i<data.length;i++){
                              HTML += "<tr><td><input type = 'text' size = '3' name = 'status[]' id = 'status[]' value=" + data[i].status + " readonly></td>";
                              HTML += "<td><input type = 'text' size = '5' name = 'lote[]' id = 'lote[]' value=" + data[i].lote + " readonly></td>";
                              HTML += "<td><input type = 'text' size = '10' name = 'endereco[]' id = 'endereco[]' value=" + data[i].endereco + " readonly></td>";
                              HTML += "<td><input type = 'text' size = '6' name = 'validade[]' id = 'validade[]' value=" + data[i].validade + " readonly></td>";
                              HTML += "<td><input type = 'text' size = '2' name = 'qtde[]' id = 'qtde[]' value=" + data[i].qtde + " readonly></td>";
                              HTML += "<td><input type = 'number' style = 'width: 25px;' name = 'qtde_trans[]' id = 'qtde_trans[]' class='input'></td></tr>";
                            }

Example of the table that loads according to the bank's data:

">

asked by anonymous 16.06.2017 / 22:17

2 answers

3

You can use .find() and .closest() to look only at the line where change happens. It would look like this:

function calc_dif() {
  var qtde = $(this).closest('tr').find('[name="qtde[]"]');
  alert(qtde.val());
}

$(function() {
  $('#locais').on('change', '.input', calc_dif);
  $("#qtde_trans").on('change', calc_dif);
});
    
16.06.2017 / 22:40
0

I usually work like this, it would not be recommended to take the value with each () just by returning from the whole list, in this case I put a id in each td I did this, and I get the value of the respective line ( tr ) clicked as in the snipt below, and I mapped the change env of qtdtrans to name by id would not work, I tried mount closer to your code, see if you can adapt

$(document).ready(function(){
$("input[name='qtde_trans']").change(function(){
var qtdTrans = $(this).val();
var qtd = $(this).parent("td").parent("tr").find("td[id=tdQtd] input").val();
alert(qtd);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<thead>
<tr>
<th>Qtd</th>
<th>Qtd Trans</th>
</tr>
<thead>
<tbody>
<tr>
   <td id='tdQtd'>
      <input type = 'text' size = '2' name = 'qtde' value='2000'>
   </td>
<td>
   <input type = 'number' style = 'width: 25px;' name='qtde_trans' class='input'>
   </td>
</tr>
<tr>
   <td id='tdQtd'>
      <input type = 'text' size = '2' name = 'qtde' value='5000'>
   </td>
<td>
   <input type = 'number' style = 'width: 25px;' name='qtde_trans' class='input'>
   </td>
</tr>
<tbody>
</table>
    
16.06.2017 / 22:46