如何在Jquery数据表中使用带有参数的href调用javascript函数添加链接?

我想从datatable列中调用editEmployee / deleteEmployee函数Edit / Delete Link with Parameter IdId value。

如何在通过“编辑和删除”链接调用的editEmployee / deleteEmployee函数中传递empId列值。

下面是我的JavaScript代码:

table = $('#employeesTable').DataTable(
                {
                    "sAjaxSource" : "/SpringDemo/employees","sAjaxDataProp" : "","order" : [ [ 0,"asc" ] ],"aoColumns" : [
                            {
                                "classname": "dt-center","sClass" : "center","mData" : "empId"
                            },{
                                "orderable": false,data: null,classname: "dt-center",defaultContent: '<a href="javascript:editEmployee(empId);" class="glyphicon glyphicon-pencil text-primary"></a>'
                            },defaultContent: '<a href="javascript:deleteEmployee(empId);" class="glyphicon glyphicon-remove text-danger"></a>'
                            } ]
                })
meng0827 回答:如何在Jquery数据表中使用带有参数的href调用javascript函数添加链接?

table = $('#employeesTable').DataTable(
{
    "sAjaxSource" : "/SpringDemo/employees","sAjaxDataProp" : "","order" : [ [ 0,"asc" ] ],"aoColumns" : [
            {
                "className": "dt-center","sClass" : "center","mData" : "empId"
            },{
                "orderable": false,data: null,className: "dt-center",defaultContent: '<a data-emp_id="[add emp id value here]" class="glyphicon glyphicon-pencil text-primary edit-link"></a>'
            },defaultContent: '<a data-emp_id="[add emp id value here]" class="glyphicon glyphicon-remove text-danger delete-link"></a>'
            } ]
})

 $('.edit-link').on('click',function () {
  var empid= $(this).data('emp_id')
  window.location.href="edit url ";
  });

 $('.delete-link').on('click',function () {
   var empid= $(this).data('emp_id')
   window.location.href="delete url ";
 });

基本上,每个类都需要具有onclick事件。然后获取存储在被单击按钮的data-emp_id属性中的empID,然后适当地重定向

,

感谢所有评论。

我已经解决了问题,方法是将ID存储在隐藏的输入中,然后像这样在编辑中访问它。

$(document).on('click','#employeesTable tr',function(e) {
    var row_object = table.row(this).data();
    $("#hdnID").val(row_object.empId);
});

function editEmployee() {
    $.ajax({
        url : 'edit/' + $("#hdnID").val(),type : 'GET',success : function(result) {
            // Do something with the result
        }
    });
}
本文链接:https://www.f2er.com/3137996.html

大家都在问