javascript - 使用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 方法。我错过了什么?谢谢。
解决方案
阅读评论后,我得到了这个最终解决方案:
<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>
感谢所有帮助过的人!
推荐阅读
- javascript - 在 > 或之间切换
我想根据我在 select option 中所做的选择来比较两个输入的数量。你能帮我怎么做吗?
solr - 需要能够从两个 Solr 集合中获取数据- python-3.x - 视图返回一个包含字典的列表,而不是 django api 视图中的字典本身
- r - R data.table 列名的自动完成在 Jupyter Notebook / Lab 中不起作用
- python - Python 无法在 Ubuntu 上导入带下划线的模块?
- python - 如何通过蓝牙将 MES 事件从 python 发送到 micro:bit
- arrays - 在没有空格的列顶部将结果分组在一起
- python - 将描述符或属性与 classmethod 一起使用
- php - PHP、NetSuite API、获取报告
- c - 解决链表实现中的“heap-use-after-free”错误