按钮链接don';t使用jquery激发数据表内部的事件


Button link don't fire the event inside data table with jquery

我有一些JQuery,它进行Ajax调用并创建数据表,这个有一个带按钮链接的列,这个按钮应该触发事件,但永远不要触发事件,而是进行回发。这是我的密码。

帮助

$(document).ready(function() {
//Trigger event
$('#datatable_tabletools tbody td a').click('click', function (e) {
            e.preventDefault();
            // show modal dialog
            // Code
        });
//Load info to datatable
$("#btn-search").click(function(e){
            e.preventDefault();
            $.ajax({
                url: '../appointment' ,
                dataType: 'json',
                type: 'get',
                data: {date : $('#date').val()},
                success: function(data){
                    var display_results = $("#table1");
                    display_results.empty();
                    display_results.html("Loading...");
                    var results = '<table id="datatable_tabletools" class="table table-striped table-hover">';
                        results += '<thead> <tr> <th>Time</th> <th>Date</th> <th>Company</th>';
                        results += '<th></th> </tr> </thead> <tbody>';
                    if (data.length != 0)
                    {
                        $.each(data, function() {
                            results += '<tr>';
                            results +='<td>' + this.date + '</td>';
                            results +='<td>' + this.time + '</td>';
                            results +='<td>' + this.company + '</td>';
                            results +='<td>';
                            results +='<a href="" data-bubble="' + this.id + ' class="btn btn-xs btn-primary">More...</a>';
                            results +='</td>'; 
                            results +='</tr>';  
                            });
                        results += '</tbody></table>';
                        display_results.empty();
                        display_results.append(results);
                        datatables();
                    } 
                },
                error: function(jqXHR, textStatus, errorThrown) {
                    alert(errorThrown);
                }
            });
        });
        /* TABLE TOOLS */
        function datatables()
        {
            $('#datatable_tabletools').dataTable({
                "sDom" : "<'dt-top-row'Tlf>r<'dt-wrapper't><'dt-row dt-bottom-row'<'row'<'col-sm-6'i><'col-sm-6 text-right'p>>",
                "aaSorting": [[ 0, "asc" ]],
                "sScrollX": "100%",
                "oTableTools" : {
                    "aButtons" : ["copy", "print", {
                        "sExtends" : "collection",
                        "sButtonText" : 'Save <span class="caret" />',
                        "aButtons" : ["csv", "xls", "pdf"]
                    }],
                    "sSwfPath" : "assets/js/plugin/datatables/media/swf/copy_csv_xls_pdf.swf"
                },
                "fnInitComplete" : function(oSettings, json) {
                    $(this).closest('#dt_table_tools_wrapper').find('.DTTT.btn-group').addClass('table_tools_group').children('a.btn').each(function() {
                        $(this).addClass('btn-sm btn-default');
                    });
                }
            });
        };
    /* END TABLE TOOLS */
})

似乎在$('#datatable_tabletools tbody td a').click('click', function (e) {中绑定了click事件,甚至在元素存在之前。。

选项1:将事件绑定更改为

$('#table1').on('click', '#datatable_tabletools tbody td a' , function (e) {
        e.preventDefault();
        // show modal dialog
        // Code
    });

选项2:

我建议在初始化数据表之后尝试移动事件绑定代码。

所以我可能会按照以下方式来做。

function dummyBinding(){
    $('#datatable_tabletools tbody td a').click('click',  function (e) {
        e.preventDefault();
        // show modal dialog
        // Code
    });
}

并在CCD_ 4函数的END调用CCD_。


我更喜欢选项1。