函数从同一类的动态列表


Function from same class of dynamic list

我正在处理一个项目列表,我需要在单击按钮时设置一个函数。列表是由php脚本动态填充的,问题是,当我单击按钮时,第一个条目是ok的(显示正确的信息),但其他显示来自第一项的信息。

<div class="col-lg-12">
<table class="table">
 <thead>
  <th>
    Nombre Local
  </th>
  <th>
    Direccion
  </th>
  <th>
    Hora de cierre
  </th>
  <th>
    Informacion
  </th>
 </thead>
 <tbody>
 <?php foreach($list as $item){
  echo '<tr>';
  echo '<td>'.$item['name'].'</td>';
  echo '<td>'.$item['addr'].'</td>';
  echo '<td>'.$item['closing'].'</td>';
  echo '<td><button class="btn btn-data detalle" data-id="'.$item['id'].'" data-tipo="'.$item['tipo'].'" onclick="GetLocalesMain()"</td>';
  echo '</tr>';
  '};?>
 </tbody>
 </table>
</div>

JS

 function GetLocalesMain(){
  var informacion =  $(".detalle");
  var id = informacion.data('id');
  var tipo = informacion.data('tipo');
  console.log(id);
  console.log(tipo);
  $.ajax({
    url: '../functions/procesa.php?item=' + id + '&tipo=' + tipo,
    type: 'POST',
    dataType: 'json',
    data: {},
    complete: function (xhr, textStatus){
    },
    success: function(data, textStatus, xhr){
     console.log('json', data);
     $(data).each(function(a){
       muestraData(this);
     });
    },
    error: function(xhr, textStatus, errorThrown) {
    }
  });
}

问题是,你得到".detalle"的所有实例,例如var informacion = $(".detalle");,但你需要的目标项目点击,你可以做到这一点很简单使用jquery(因为你正在使用它)。

你可以在你的html中删除onclick="GetLocalesMain()",并在javascript中放置GetLocalesMain函数的内容在以下jquery点击函数:

$( ".detalle").click(function() {
  // function contents goes here
});

var informacion = $(".detalle");替换为var informacion = $(this);

您现在应该得到被单击的项和随后的正确数据。