【问题标题】:Returning SQL Insert ID when using Jquery/AJAX to insert row into database使用 Jquery/AJAX 将行插入数据库时​​返回 SQL 插入 ID
【发布时间】:2009-10-30 11:06:33
【问题描述】:

我正在尝试向数据库添加一个元素,然后根据 sql_insert_id 为其返回一个新 id

它正确添加了元素,控制台返回了我想使用的 id,但由于某种原因,它实际上并没有在元素上更改它。

我只是在我的 actions.php 脚本中使用 echo 来返回字符串。

$(".add").click(function () {
    $(this).parent().parent().parent().parent().appendTo("#folderwrap");
    $(".thumbnailoverlay").hide();
    var imageidlong = $(this).parent().parent().parent().parent().attr("id");
    var imageid = imageidlong.replace("imageid_", "");
    $.post("actions.php?action=addfolderimage&imageid="+imageid, function(data){
        console.log(data);
        $(this).parent().parent().parent().parent().attr("id", data);
    });
});

【问题讨论】:

    标签: php jquery mysql ajax


    【解决方案1】:

    在您的回调中 $(this) 将不再引用您的元素 - 请尝试以下操作:

    $(".add").click(function () {
            var elm = $(this).parent().parent().parent().parent();
    
            elm.appendTo("#folderwrap");
            $(".thumbnailoverlay").hide();
            var imageidlong = elm.attr("id");
            var imageid = imageidlong.replace("imageid_", "");
    
            $.post("actions.php?action=addfolderimage&imageid="+imageid, function(data){
                    console.log(data);
                    elm.attr("id", data);
            });
    });
    

    这会创建一个包含elm的闭包

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多