dqjjw04440 2015-02-26 23:25 采纳率: 0%
浏览 575
已采纳

通过JQuery获取动态内容的ID

I'm creating some dynamic content tabs using PHP, one object on the tab is a datatable. I define the ID via PHP like this:

PHP:

echo '<table class="table table-striped table-bordered table-hover" id="'.$nospaces.'">';

The table loads the data perfectly however I'm having problems initializing the datatable for sorting and filtering. Normally on static tables I would just initialize it using the static ID but I can't do that in this case.

I thought I could do some jquery to listen to a tab click event and then grab the table ID and load it into the jquery datatables initialization but that isn't working either. I'm just getting an undefined alert:

JQuery:

<script>
    $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
    var row = $(this).closest('table').attr('id');
    alert(row);

    $(document).ready(function() {
    oTable = $('row').dataTable( {
       "sDom": '<"top"lf>rt<"bottom"ip><"clear">'

    });

    });
    });
</script>

Any help would be greatly appreciated! Thanks.

  • 写回答

3条回答

  • doudou0111 2015-03-02 18:31
    关注

    I ended up doing a mixture with some extra steps. Not the cleanest but it's working.

    First on the dynamic tab/table I created a variable for a new ID because I had the tab and the table ID the same:

    PHP:

    $tableid = $nospaces .'table';
    echo '<table class="table table-striped table-bordered table-hover" id="'.$tableid.'">';
    

    I then initialize (and destroy) the datatable on tab click like this:

    JQuery:

    $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
    tab = e.target.hash // newly activated tab
    tabtable = tab + 'table'
    
    $(document).ready(function() {
        oTable = $(tabtable).dataTable( {
            "sDom": '<"top"lf>rt<"bottom"ip><"clear">'
        });
    });
    $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
        oldtab = e.relatedTarget.hash
        oldtable = oldtab + 'table'
        $(oldtable).dataTable().fnDestroy();
        });
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题