社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Jquery

使用jquery从动态按钮获取ID

froz • 6 年前 • 969 次点击  

我有从数据库获取值的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

我想当我点击这个按钮时,我会从他们基于PHP值的ID中得到警报。 请给我建议。

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/30862
 
969 次点击  
文章 [ 1 ]  |  最新文章 6 年前
Lawrence Cherone
Reply   •   1 楼
Lawrence Cherone    6 年前

不要使用 id 代替使用 data- 属性,然后使用类以按钮为目标,例如:

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

然后在jquery中,可以使用数据API获取值。

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

我忽略了以下几点:

  • 脚本标记错误 <script>src="jquery-3.3.1.js"</script>
  • 遗失 <tbody>
  • 注意它们的自动“的”,有些编辑只是在你打字的时候加进去的,以保持你的警觉。