【问题标题】:onClick function for button inside jquery DataTablejquery DataTable中按钮的onClick函数
【发布时间】:2019-03-31 10:33:27
【问题描述】:

我的 jquery DataTable 中的每一行都有一个按钮。我想在我点击按钮时提醒所选记录的第一列的值。

问题是,我的按钮代码不起作用。

这是我遵循的文档:https://datatables.net/examples/ajax/null_data_source.html

这是我的代码:

jQuery

$(document).ready(function () {
    $.ajax({
        type: "POST",
        url: "WebService1.asmx/Bind",
        dataType: 'json',
        success: function (data) {
            $('#datatable').DataTable({
                data: data,
                columns: [
                    { 'data': 'CenterID' },
                    { 'data': 'CenterName' },
                    { 'data': 'LicenseKey' },
                    { 'data': 'ExpiryDate' },
                    { 'data': 'YearID' },
                    { 'data': 'Date' },
                    // This is where I add the button
                    { "defaultContent": "<button>Edit</button>"}
                ]
            });
        }

    });
    //This is where I make an attempt to add functionality for the button in the datatable
    $('#datatable tbody').on('click', 'button', function () {
        var data = table.row($(this).parents('tr')).data();
        alert("The value is: " + data[0]);
    });
});

HTML

<table id="datatable" class="display" style="width: 100%">
            <thead>
                <tr>
                    <th>CenterID</th>
                    <th>CenterName</th>
                    <th>LicenseKey</th>
                    <th>ExpiryDate</th>
                    <th>YearID</th>
                    <th>Date</th>
                    <th>Action</th>
                </tr>
            </thead>
        </table>

请帮忙。谢谢。

【问题讨论】:

  • 检查点击按钮时浏览器检查元素是否有任何错误。
  • 不,没有错误。我已经检查过了。只是回了一个帖子,它没有到达我的 js 代码。

标签: javascript jquery datatable


【解决方案1】:

所以经过大量研究,我明白了。我不得不更改按钮功能。

$('#datatable tbody').on( 'click', 'button', function () {
     var tr = $(this).closest('tr');
     var value = $(tr).find('td').eq(0)[0].innerHTML;
     alert(value);
     return false;
});

如果有人有更好的方法,请告诉我。

【讨论】:

    猜你喜欢
    • 2014-12-22
    • 1970-01-01
    • 1970-01-01
    • 2020-10-08
    • 2016-03-28
    • 2020-08-17
    • 1970-01-01
    • 2015-05-14
    • 2013-03-23
    相关资源
    最近更新 更多