首页 > 解决方案 > 使用ajax,php在单击同一页面上获取ID

问题描述

简介:我正在使用 PHP 和 Ajax 做一个服务器端数据表库。当用户单击编辑按钮时,我想将该 ID 传输到 PHP,以便稍后更新它...

我基本上将这两篇堆栈溢出文章结合起来:

索引.php

<script type="text/javascript"> 
$(document).on('click','.edit_btn',function (){
var edit_id = $(this).attr("id");
alert(edit_id);
    $.ajax({
         url: 'index.php',
      data: { edit_id : edit_id },
        contentType: 'application/json; charset=utf-8',
        success: function(result) {
              alert(result);
        }
    });
});
</script>

编辑按钮代码:

columnDefs:
[
    {  
        targets: -1,
        render: function (data, type, row, meta) {
            return '<input type="button" class="delete_btn" id=n-"' + meta.row + '" value="delete"/> <input type="button" class="edit_btn" id=s-"' + meta.row + '" value="edit" name="edit_btn"> </a>';
        }
    }
]

我相信数据表库不建议使用 URL 中的 get 方法。我错过了什么?谢谢。

标签: javascriptphpjqueryajaxdatatable

解决方案


阅读评论后,我得到了这个最终解决方案:

 <script type="text/javascript"> 
    $(document).on('click','.edit_btn',function (){
          var id = $(this).attr("id").match(/\d+/)[0];
            var edit_id = $('#example').DataTable().row( id ).data();
            var edit_id = edit_id[0];
    alert(edit_id);
        $.ajax({
             url: 'index.php',
          data: { edit_id : edit_id },
            contentType: 'application/json; charset=utf-8',
            success: function(result) {
                  //alert(result);
            }
        });
    });
    </script>

感谢所有帮助过的人!


推荐阅读