从值id获取id

时间:2018-06-05 02:40:30

标签: javascript jquery html

如何获取此id的值,我从控制器发送它并将其发送到表中

datail

我的HTML

<tbody class="no-border-x">
   <tr>
     <td id="id"></td>
     <td id="nm_kegiatan"></td>
     <td id="biaya_pemeliharaan"></td>
     <td id="tgl_pemeliharaan"></td>
     <td id="penambahan_nilai_aset"></td>
     <td><a onclick='deleteKegiatan(this.id)' class="pointer-jempol"><i class="mdi mdi-delete"></i></a></td>
   </tr>
 </tbody>

我的脚本加载表格控制器

function showModal(id) { 
$('input[name=_method]').val('PATCH');
$('#modal-form form')[0].reset();
$.ajax({
  url: "{{ url('pemeliharaan/kegiatan') }}/" + id, //menampilkan data dari controller edit
  type: "GET",
  dataType: "JSON",
  success: function (data) { 
    var d = data[0];
    $('#modal-form').modal('show');
    $('#id').html(d.id);
    $('#nm_kegiatan').html(d.nm_kegiatan);
    $('#biaya_pemeliharaan').html(d.biaya_pemeliharaan);
    $('#tgl_pemeliharaan').html(d.tgl_pemeliharaan);
    $('#penambahan_nilai_aset').html(d.penambahan_nilai_aset);

  },
  error: function () {
    alert("Data tidak ada");
  }

});
}

1 个答案:

答案 0 :(得分:3)

this.id未引用<td id='id'>。它指的是被点击元素的id。并且id应该是唯一的,因此您不能在表格的每一行都有<td id="id"></td>

  • 在删除delete-row
  • 上添加课程<a>
  • class s
  • 上使用id代替<td>

&#13;
&#13;
$("body").on('click', '.delete-row', function(event) {
  var id = $(this).parents('tr').find('.id').text();
  console.log(id);
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tbody class="no-border-x">
    <tr>
      <td class="id">1</td>
      <td class="nm_kegiatan"></td>
      <td class="biaya_pemeliharaan"></td>
      <td class="tgl_pemeliharaan"></td>
      <td class="penambahan_nilai_aset"></td>
      <td><a class="pointer-jempol delete-row"><i class="mdi mdi-delete"></i> Delete</a></td>
    </tr>
    <tr>
      <td class="id">2</td>
      <td class="nm_kegiatan"></td>
      <td class="biaya_pemeliharaan"></td>
      <td class="tgl_pemeliharaan"></td>
      <td class="penambahan_nilai_aset"></td>
      <td><a class="pointer-jempol delete-row"><i class="mdi mdi-delete"></i> Delete</a></td>
    </tr>
    <tr>
      <td class="id">3</td>
      <td class="nm_kegiatan"></td>
      <td class="biaya_pemeliharaan"></td>
      <td class="tgl_pemeliharaan"></td>
      <td class="penambahan_nilai_aset"></td>
      <td><a class="pointer-jempol delete-row"><i class="mdi mdi-delete"></i> Delete</a></td>
    </tr>
  </tbody>
</table>
&#13;
&#13;
&#13;