使用JQuery从动态按钮获取ID

问题描述 投票:3回答:1

我有php文件从数据库获取值。我无法从结果中获得动态的id按钮。我做错了吗?

这是我的代码

<!DOCTYPE HTML>
<html>
<head>
    <link rel="stylesheet" href="css/bootstrap.min.css">
    <script>src="jquery-3.3.1.js"</script>
</head>
<body>
    <div class="container">
        <?php
        include("koneksi.php");
        $sql = "SELECT * FROM data_cv";
        $result = $conn->query($sql);
        ?>
        <div class="table-responsive">
        <table class="table table-bordered">
            <thead>
                <tr>
                    <th>Nama</th>
                    <th>Nomor Identitas</th>
                    <th>Tempat Lahir</th>
                    <th>Tanggal Lahir</th>
                    <th>Jenis SIM</th>
                    <th>Masa SIM</th>
                    <th>Nomor SKCK</th>
                    <th>Pendidikan</th>
                    <th>Nomor Telepon (Handphone)</th>
                    <th>Keterangan</th>
                    <th>Terima Berkas</th>
                    <th>Tindakan</th>
                </tr>
            </thead>
            <?php
            while ($row = mysqli_fetch_array($result)) {
                echo '
                <tr>
                <td>'.$row["nama"].'</td>
                <td>'.$row["id_ktp"].'</td>
                <td>'.$row["tempat_lahir"].'</td>
                <td>'.$row["tanggal_lahir"].'</td>
                <td>'.$row["jenis_sim"].'</td>
                <td>'.$row["masa_sim"].'</td>
                <td>'.$row["no_skck"].'</td>
                <td>'.$row["pendidikan"].'</td>
                <td>'.$row["no_telp"].'</td>
                <td>'.$row["keterangan"].'</td>
                <td>'.$row["terima_berkas"].'</td>
                <td><button id= "'.$row['id_ktp'].'" value="Accept"">Panggil</button></td>
                </tr>
                ';
            }
            $conn->close();
            ?>
            <script>
            $("button").click(function() {
            alert(this.id);
            });
        </table>
        </div>
    </div>
</body>
</html>

我的php文件结果是这个

php file result

我想当我点击按钮时,我从他们的id基于php值得到警报。请给我建议。

php jquery html
1个回答
2
投票

不要使用qazxsw poi而是使用qazxsw poi属性,然后使用类来定位按钮,例如:

id

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

data-

我忽略了以下几点:

  • 脚本标签是错误的<button data-id="'.$row['id_ktp'].'" class="valueButton"></button>
  • 缺少<script> $(".valueButton").click(function() { alert($(this).data('id')); }); </script>
  • 并小心使用它们的汽车“”“,一些编辑只是在你打字时添加,以保持你的脚趾。
© www.soinside.com 2019 - 2024. All rights reserved.