douzhang6646 2014-04-09 22:43
浏览 30
已采纳

按钮链接不会使用jquery在数据表中触发事件

I have some JQuery that makes an Ajax call and crete a datatable, this one has a column with button link, this button should trigger an event, but never fire the event, instead makes a postback. This is my code.

Help

$(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 */

})
  • 写回答

1条回答 默认 最新

  • doqau82086 2014-04-09 22:54
    关注

    It seems you are binding the click event in $('#datatable_tabletools tbody td a').click('click', function (e) { even before the element exists..

    Option 1: Change your event binding to

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

    Option 2:

    I would suggest try moving your event binding code AFTER the datatable is initialized.

    So probably I would do it the following way.

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

    and call dummyBinding() function at the END of the datatables() function.


    I would prefer the option 1.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP