简体   繁体   English

Javascript函数未从Jquery Datatables行触发

[英]Javascript function not triggering from Jquery Datatables row

I have a table that uses datatables and I want to trigger a function when I select a link on a row like this: 我有一个使用数据表的表,当我在这样的行上选择一个链接时,我想触发一个函数:

<script>
        var selected = Array();
        var form_data = {
            "Id": "@Model.ApplicationDetails.Id"
        };
        $(document).ready(function () {
            var table = $("#table").DataTable({
                "ajax": {
                    "url": "@Url.Action("GetApplicationRoles", @ViewContext.RouteData.Values["controller"].ToString())",
                    "type": "POST",
                    "data": form_data,
                    "datatype": "json"
                },
                "filter":false,
                "language": {
                    "search": "",
                    "searchPlaceholder": " Search"
                },
                "ordering": true,
                "lengthChange": false,
                "columns": [
                    {
                        "data": function (data, type, row, meta) {
                             var url = "@Url.Action("EditRole", @ViewContext.RouteData.Values["controller"].ToString())/" + data.Id;
                            return "<a href='#' class='select-item' data-id='"+data.Id+"'>" + data.RoleName + "</i></a>"
                        }, "name": "RoleName"
                    }
                ],
                "responsive": true,
                "processing":true,
                "serverSide": true,
            }).columns.adjust()
            .responsive.recalc();
            new $.fn.dataTable.FixedHeader(table);
        });

        $("#table tbody").on('click',".select-item",function (e) {
            e.preventDefault();
            alert("test");
            return false;
            //e.preventDefault();
            //console.log($(this).data("id"));
        });
    </script>

As you can see, when a user clicks on this row: 如您所见,当用户单击此行时:

<a href='#' class='select-item' data-id='"+data.Id+"'>" + data.RoleName + "</i></a>

The function at the bottom should trigger. 底部的功能应触发。 However, right now the only thing that happens is that my url is appended by a hash#. 但是,现在唯一发生的是我的URL附加了#号。 I think I missed something but I can't seem to catch it. 我想我错过了一些东西,但似乎无法抓住。

You're using a delegated event handler, which is correct. 您使用的是委托事件处理程序,这是正确的。 However one issue is because the primary selector needs to be an element that is present in the DOM when the page loads. 但是,一个问题是因为主选择器需要是页面加载时DOM中存在的元素。 The tbody element is also dynamically generated, so the event handler is not attached correctly. tbody元素也是动态生成的,因此事件处理程序未正确附加。

To fix this, attach the event to the #table directly: 要解决此问题,请将事件直接附加到#table

$('#table').on('click', '.select-item', function(e) {
  e.preventDefault();
  alert("test");
});

You also need to move that code block inside the document.ready event handler. 您还需要在document.ready事件处理程序移动该代码块。

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

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