php-使用JQuery从动态按钮获取ID

前端之家收集整理的这篇文章主要介绍了php-使用JQuery从动态按钮获取ID 前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我有PHP文件,可从数据库获取价值.
我很难从结果中获得一个动态的id按钮.
我做错了吗?

这是我的代码

  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <link rel="stylesheet" href="css/bootstrap.min.css">
  5. <script>src="jquery-3.3.1.js"</script>
  6. </head>
  7. <body>
  8. <div class="container">
  9. <?PHP
  10. include("koneksi.PHP");
  11. $sql = "SELECT * FROM data_cv";
  12. $result = $conn->query($sql);
  13. ?>
  14. <div class="table-responsive">
  15. <table class="table table-bordered">
  16. <thead>
  17. <tr>
  18. <th>Nama</th>
  19. <th>Nomor Identitas</th>
  20. <th>Tempat Lahir</th>
  21. <th>Tanggal Lahir</th>
  22. <th>Jenis SIM</th>
  23. <th>Masa SIM</th>
  24. <th>Nomor SKCK</th>
  25. <th>Pendidikan</th>
  26. <th>Nomor Telepon (Handphone)</th>
  27. <th>Keterangan</th>
  28. <th>Terima Berkas</th>
  29. <th>Tindakan</th>
  30. </tr>
  31. </thead>
  32. <?PHP
  33. while ($row = MysqLi_fetch_array($result)) {
  34. echo '
  35. <tr>
  36. <td>'.$row["nama"].'</td>
  37. <td>'.$row["id_ktp"].'</td>
  38. <td>'.$row["tempat_lahir"].'</td>
  39. <td>'.$row["tanggal_lahir"].'</td>
  40. <td>'.$row["jenis_sim"].'</td>
  41. <td>'.$row["masa_sim"].'</td>
  42. <td>'.$row["no_skck"].'</td>
  43. <td>'.$row["pendidikan"].'</td>
  44. <td>'.$row["no_telp"].'</td>
  45. <td>'.$row["keterangan"].'</td>
  46. <td>'.$row["terima_berkas"].'</td>
  47. <td><button id= "'.$row['id_ktp'].'" value="Accept"">Panggil</button></td>
  48. </tr>
  49. ';
  50. }
  51. $conn->close();
  52. ?>
  53. <script>
  54. $("button").click(function() {
  55. alert(this.id);
  56. });
  57. </table>
  58. </div>
  59. </div>
  60. </body>
  61. </html>

我的PHP文件结果是这样

php file result

我想要当我单击按钮时,我从基于PHP值的id中获得警报.
请给我建议.

最佳答案
不要使用id而是使用data-属性,然后使用类来定位按钮,例如:

< button data-id =“'.$row ['id_ktp'].'” class =“ valueButton”>< / button>

然后在您的jQuery中,您可以使用数据API获取值.

  1. <script>
  2. $(".valueButton").click(function() {
  3. alert($(this).data('id'));
  4. });
  5. </script>

我忽略了以下内容

>脚本标记错误< script> src =“ jquery-3.3.1.js”< / script>
>丢失< tbody>
>并小心使用auto的“”“,某些编辑器会在您键入时添加它们,以使您始终保持警惕.

猜你在找的HTML相关文章