【问题标题】:JQuery (this).data("id") returns undefined output [duplicate]JQuery(this).data(“id”)返回未定义的输出[重复]
【发布时间】:2021-09-30 14:32:05
【问题描述】:

我有两个文件,load-request.php 和 load-data.php,当我单击删除按钮获取其值时,load-request.php 中的删除按钮返回一个未定义的值。 请告知,为什么我没有得到价值。

加载请求.php

<form>
    <table class="table" id="main" border="0" cellspacing="0">
        <tr>
            <td id="table-load">
                <input type="button" id="load-button" value="Load Data">
            </td>
        </tr>
        <table class="table" width="55%">
            <tr>
                <td id="table-data"></td>
            </tr>
        </table>
        </td>
        </tr>
    </table>
    <script>
        $(document).ready(function() { //ajax to load the table
            $("#load-button").on("click", function() {
                $.ajax({
                    url: "load-data.php",
                    type: "POST",
                    dataType: "text",
                    success: function(data) {
                        $("#table-data").html(data);
                    }
                });
            });
            $(document).on("click", ".delete-btn", function() {
                console.log($(this).find("data-id"));
                var pn = $(this).attr('value');
                alert(pn);

            });
        });
    </script>
</form>
</body>
</html>

加载数据.php

<?php
while ($row = mysqli_fetch_assoc($result)) {
    $output .= "<tr><td>{$row["pname"]}</td><td>{$row["src"]}</td><td>{$row["dst"]}</td><td>{$row["ports"]}</td><td>{$row["inzone"]}</td><td>{$row["outzone"]}<td><button Class='delete-btn' **data-id'{$row["pname"]}**'>Delete</button></td></tr>";
}
$output .= "</table>";
echo $output;
mysqli_close($conn);
?>

【问题讨论】:

  • 欢迎来到 Stack Overflow!从你的 php 看你的 HTML 是什么样的?您似乎缺少=data-id={$row["pname"]}
  • 我想你错过了一个 =
  • 添加了这个 = 但结果仍然相同,从处理数据库连接和事务的 load-data.php 中,我可以在 load-request.php 中获取表中的所有记录,唯一的问题是在 load-data.php 中创建的按钮不返回值。 while ($row = mysqli_fetch_assoc($result)) { $output .= " {$row["pname"]} {$row["src"]} td> {$row["dst"]} {$row["ports"]} {$row["inzone"]} {$row["outzone"]} .

标签: javascript php jquery ajax


【解决方案1】:

自从我不使用 jQuery 以来已经有一段时间了,但我可以告诉你,如果在您注册事件的那一刻,一个元素没有附加到 DOM,它就不会订阅它。所以也许在你渲染数据之后你可以注册事件,这样元素就会监听它。像这样的:

$("#load-button").on("click", function() {
    $.ajax({
        url: "load-data.php",
        type: "POST",
        dataType: "text",
        success: function(data) {
            $("#table-data").html(data);
            $(document).on("click", ".delete-btn", function() {
              console.log($(this).find("data-id"));
              var pn = $(this).attr('value');
              alert(pn);

          });
        }
    });
});

【讨论】:

  • 仍然与您的渲染建议相同的错误:(
【解决方案2】:

Jquery.ajax 默认为async。你可以在Jquery.ajax documentation中清楚地看到这一点

默认情况下,所有请求都是异步发送的(即设置为 默认为真)。如果您需要同步请求,请将此选项设置为 假的。

所以要解决您的问题,您需要

  1. :让它同步而不是异步:
$(document).ready(function() { //ajax to load the table
    $("#load-button").on("click", function() {
        $.ajax({
            url: "data.php",
            type: "POST",
            dataType: "text",
            success: function(data) {
                $("#table-data").html(data);
            },
            async: false
        });

        $(document).on("click", ".delete-btn", function() {
            console.log($(this));
            var pn = $(this).attr('data-id');
            alert(pn);

        });
    })
});

  1. 或者您可以收听文档对象
$(document).ready(function() { //ajax to load the table
    $("#load-button").on("click", function() {
        $.ajax({
            url: "data.php",
            type: "POST",
            dataType: "text",
            success: function(data) {
                $("#table-data").html(data);
                
            }
        });
    })
}).on("click", ".delete-btn", function() {
    console.log($(this));
    var pn = $(this).attr('data-id');
    alert(pn);
});
  1. 如下使用自定义监听器
$(document).ready(function() { //ajax to load the table

    function myCustomListener(that) {
        that.on("click", ".delete-btn", function() {
            console.log($(this));
            var pn = $(this).attr('data-id');
            alert(pn);
        });
    }

    $("#load-button").on("click", function() {
        $.ajax({
            url: "data.php",
            type: "POST",
            dataType: "text",
            success: function(data) {
                $("#table-data").html(data);

                myCustomListener($("#table-data"));
            }
        });
    })
});

【讨论】:

  • 使用此代码,它不会填充 SQL 表中的所有数据,只填充表中的一行
  • $(document).ready(function() { //ajax 加载表格 $("#load-button").on("click", function() { $.ajax( { url: "load-data.php", type: "POST", dataType: "text", 成功: function(data) { $("#table-data").html(data); } }); } ); $(document).on("click", ".delete-btn", function() { var pn = $(this).data("id" ); alert(pn); }); });
  • 我上面粘贴的那个是工作的,但还是不知道是什么问题,现在我可以在警报中看到返回值
  • 这是不返回值'='操作符的主要原因,data-id='{$row["pname"]}'
  • data-id'{$row["pname"]}' =====> data-id='{$row["pname"]}'
猜你喜欢
  • 2012-05-21
  • 1970-01-01
  • 2013-03-08
  • 1970-01-01
  • 1970-01-01
  • 2014-10-02
  • 2015-09-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多