简体   繁体   English

对象上的Javascript在DataTable行中不起作用

[英]Javascript on objects don't work in DataTable rows

I just implemented a DataTable in my app, but it seems like javascript doesn't work within the DataTable. 我只是在我的应用程序中实现了一个DataTable,但是javascript在DataTable中似乎不起作用。

I've attached all code below for better readability. 我在下面附加了所有代码,以提高可读性。

As you can see, the ccbtn_action="delete" bit is present, but Chrome/IE/FF doesn't seem to want to do anything when the glyphicon is clicked. 如您所见,存在ccbtn_action="delete"位,但是单击glyphicon时Chrome / IE / FF似乎不想执行任何操作。

This code works perfectly when called from outside the DataTable. 从DataTable外部调用时,此代码可完美工作。

What gives? 是什么赋予了? Is it something to do about JavaScript not being applied to dynamically generated elements? 是否将JavaScript不适用于动态生成的元素,这与之有关吗?

Thank you! 谢谢!

Here is the Javascript code that doesn't work: 这是无效的Javascript代码:

$(document).ready(function(){

    // Delete Records

    $('[ccbtn_action="delete"]').on('click', function() {
        var type = $(this).attr('ccbtn_value_type');
        var value = $(this).attr('ccbtn_value_id');
        console.log('type=' + type + '&id=' + value);


        if (confirm('Are you sure you want to PERMANENTLY delete this record? There is NO TURNING BACK!')) {
            $.ajax({
            type: 'POST',
            url: 'includes/crmcore_action.php?action=cc_delete',
            data: 'type=' + type + '&id=' + value,
            success: 
            function() {
                $('#cc_pagetop_status').html("<div class='alert alert-success'><strong>Success!</strong> The record was successfully deleted.</div>");

                if (type == "company")
                {
                    window.location = "companies_list.php";
                }
                else
                {
                    location.reload();
                }

            }
        });
} else {
    // Do nothing!
}

    });
});

Here is the code for the DataTable: 这是DataTable的代码:

$(document).ready(function() {
    var t = $('#DataTable').DataTable({
        "order": [[ 1, 'asc' ]],
        ajax: {
            url: 'includes/dt_ss.php?getwhat=company',
            dataSrc: ''
        },
        columns: [
        {data: null},
        {"data": null,
        "render": function (data, type, row)
            {
                return '<a href="companies_viewrecord.php?id='+data.id+'">'+data.name+'</a>';
            }
        },
        //{data: 'name'},
        {data: 'tel'},
        {
            "data": "id",
            "render": function ( data, type, full, meta )
            {
                return '<span class="glyphicon glyphicon-remove" ccbtn_action="delete" ccbtn_value_type="company" ccbtn_value_id="'+data+'" data-toggle="tooltip" data-placement="bottom" title="Click me to delete"></span>';
            }
        }
        ],
    });

     t.on( 'order.dt search.dt', function () {
        t.column(0, {search:'applied', order:'applied'}).nodes().each( function (cell, i) {
            cell.innerHTML = i+1;
        } );
    } ).draw();
});

Try delegating your event like this: 尝试像这样委派您的活动:

$('#DataTable').on('click', '[ccbtn_action="delete"]', function() { ...

My guess is the click event is attached before your ajax request loads the DataTable rows. 我的猜测是在您的Ajax请求加载DataTable行之前会附加click事件。 You can read more here about jQuery event delegation with on() . 您可以在这里阅读更多有关on() jQuery事件委托的信息 Specifically: 特别:

Event handlers are bound only to the currently selected elements; 事件处理程序仅绑定到当前选定的元素; they must exist at the time your code makes the call to .on() 它们必须在您的代码调用.on()时存在。

Since the js looks ok, this is most probably a timing issue. 由于js看起来还不错,所以这很可能是时间问题。 You part of script that binds the events is executed before the actual elements are created. 在创建实际元素之前,将执行绑定事件的脚本的一部分。

To fix that, you can: 要解决此问题,您可以:

  1. Make sure the script runs binding after elements creation 确保脚本在创建元素后运行绑定

  2. Use dynamic binding (like .delegate() http://api.jquery.com/delegate/ ) 使用动态绑定(例如.delegate() http://api.jquery.com/delegate/

尝试这样,但jquery版本必须为1.9+

$(document).on('click', '[ccbtn_action="delete"]', function() { // your remaining code 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM