简体   繁体   English

为什么 jquery 数据表不会响应点击?

[英]why won't jquery datatable respond to click?

I have a jquery datatable that populates from ajax but when I click on a row, the table click event handler does not fire.我有一个从 ajax 填充的 jquery 数据表,但是当我单击一行时,表单击事件处理程序不会触发。 Here is my datatable:这是我的数据表:

function initDataTables(watchlist) {
    $('#watchlistTable').DataTable({
        ajax: {
            url: "/MoneyMachine/getWatchlist.php",
            type: "post",
            data: {watchlist:watchlist}
        },
        columns: [
            { data: 'Symbol' },
            { data: 'CompanyName' },
            { data: 'ClosePrice', class: 'text-right' },
            { data: 'PricePctChangeToday', class: 'text-right'},
            { data: 'CAGR', class: 'text-right'},
            { data: 'BenchmarkCAGR', class: 'text-right'},
            { data: 'DivYield', class: 'text-right'},
            { data: 'ExDivDate'},
            { data: 'AppreciationPotential', class: 'text-right'}
        ],    
        responsive: true,   //supposedly make table clickable but does nothing
        destroy: true       //A data table can only be initialized once.  It must be destroyed so it can be initialized again.      
    });
}

I've tried a couple different click events like this:我尝试了几个不同的点击事件,如下所示:

$('.dataTable').on( 'click', '.dataTable', function () {
    console.log("watchlistTable was clicked");
});

And like this:像这样:

$('#watchlistTable tbody').on( 'click', 'tr', function () {
    console.log("watchlistTable was clicked");
});

And like this:像这样:

$('.display').on( 'click', '.display', function () {
    console.log("watchlistTable was clicked");
});

But neither one fires.但谁都不会火。 Here is the html for it:这是它的html:

        <table id="watchlistTable" class="display" style="width:100%">
        <thead>
            <tr>
                <th>Sym</th>
                <th>Company Name</th>
                <th>Price</th>
                <th>Change%</th>
                <th>CAGR</th>
                <th>Benchmark<br>CAGR</th>
                <th>Div<br>Yield%</th>
                <th>Ex-Div<br>Date</th>
                <th>App<br>Pot%</th>
            </tr>
        </thead>
        </table>

My guess is you aren't including a <tbody> in the original html我的猜测是您没有在原始 html 中包含<tbody>

Try adding it to the target selector and delegating to the table itself instead尝试将其添加到目标选择器并委托给表本身

$('#watchlistTable').on( 'click', 'tbody tr', function () {...

Note that $('.dataTable').on( 'click', '.dataTable', ... is trying to delegate to an existing class dataTable and would only be triggered on other elements with that class that are inside it. Since nothing in the html shown has that class it's not clear what that element would even be and I doubt you have nested elements with same class请注意, $('.dataTable').on( 'click', '.dataTable', ...试图委托给现有的类dataTable并且只会在其中包含该类的其他元素上触发。由于显示的 html 中没有任何内容具有该类它甚至不清楚该元素是什么,我怀疑您是否嵌套了具有相同类的元素

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

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