简体   繁体   English

如何解决点击事件。 即使调试没有指向它,它也不起作用

[英]how to fix on click event. it is not working even the debug is not pointing toward it

i'm creating a datatable for insert, update . 我创建一个datatableinsert, update edit event will fire on the click of but it is not firing any event on click, even the debug is not pointing toward it on click. 编辑事件将在单击时触发,但单击时不会触发任何事件,即使调试未指向单击时也是如此。

function GetLoginData() {
    $.ajax({
        url: '/LoginMaster/GetLoginData',
        type: 'get',
        dataType: 'json',
        success: function (result) {
            var GetLoginData = result;
            //$('#tblLoginMaster').DataTable.clear();
            //$('#tblLoginMaster').DataTable.destroy();
            //                table.empty();
            if (GetLoginData != '') {
                var str = "";

                $.each(GetLoginData, function (index, obj) {
                    var GetID = obj.Id == null ? "" : obj.Id;
                    var GetName = obj.Name == null ? "" : obj.Name;
                    var GetEmailID = obj.Email == null ? "" : obj.Email;
                    var GetPassword = obj.Password == null ? "" : obj.Password;
                    var GetRole = obj.Role == null ? "" : obj.Role;

This is where my editstyle class is 这是我的editstyle类所在的地方

                    str += "<tr class='EditStyle' data-target='#addUserModal' data-toggle='modal' value='" + GetID +"'>";
                    str += "<td>" + GetID + "</td>";
                    str += "<td>" + GetName + "</td>";
                    str += "<td>" + GetEmailID + "</td>";
                    str += "<td>" + GetPassword + "</td>";
                    str += "<td>" + GetRole + "</td>";
                    str += "</tr>"
                    $('#tblbodyLoginMaster').html(str);
                });
                $('#tblLoginMaster').DataTable({
                    "paging": true,
                    "lengthChange": false,
                    "searching": false,
                    "ordering": true,
                    "info": true,
                    "autoWidth": false,
                    "sDom": 'lfrtip'
                });
            }
        }
    });
}

This is my click event 这是我的click event

$('tr.EditStyle').on('click', function () {
    if ((this).val() != 0) {
        var rowData = (this).children('td').val();

        $('#id').val(rowData[0]);
        $('#username').val(rowData[1]);
        $('#emailId').val(rowData[2]);
        $('#password').val(rowData[3]);
        $('#role').val(rowData[4]);
    }
});

I'm suspecting your click handler doesn't work as your rows do not exist by the time you attempt to listen for clicks. 我怀疑您的点击处理程序不起作用,因为在您尝试监听点击时您的行不存在。 Try to change that, like this: 尝试更改它,如下所示:

$('#tblLoginMaster').on('click', 'tr.EditStyle', function () {
    ...
});

If that solves your problem, you may check out oficial tutorial on that topic. 如果这样可以解决您的问题,则可以查看有关该主题的官方教程

Another thing in your code that raises my concern is using jQuery $.ajax() method to populate your table and the very method you populate that: 代码中引起我关注的另一件事是使用jQuery $.ajax()方法填充表以及填充该表的方法:

  • in order to fetch your data dynamically, you should use DataTables ajax option and take the advantage of ajax.dataSrc : 为了动态获取数据,您应该使用DataTables ajax选项并利用ajax.dataSrc的优势:
$('#tblLoginMaster').DataTable({
    ...,
    ajax: {
        url: '/LoginMaster/GetLoginData',
        method: 'GET',
        dataSrc: //here goes json property or the method that retrieves table data out of json response
    }
});
  • you don't need to cook up table body HTML on your own, you may simply point to corresponding source data properties within columns / columnDefs options, and make use of createdRow option to assign necessary row attributes: 您不需要自己准备表主体HTML,您可以简单地指向columns / columnDefs选项中的相应源数据属性,并利用createdRow选项分配必要的行属性:
$('#tblLoginMaster').DataTable({
    ...,
    rowCreated: (tr, _, rowIdx) => $(tr).attr({
        'class':'EditStyle', 
        'data-target': '#addUserModal', 
        'data-toggle': 'modal', 
        'value': rowIdx
    }),
    columns: [
        {data: 'Id', header: 'Id'},
        {data: 'Name', header: 'Name'},
        {data: 'Password', header: 'Password'},
        ...
    ]
});

You may be reluctant to do such a global change to your app, but otherwise you may run into different sorts of issues related to AJAX-async nature and deprive yourself of the possibility to use the plethora of DataTables API methods to catch the bugs or enrich your app with new features. 您可能不愿意对应用程序进行这样的全局更改,但否则,您可能会遇到与AJAX异步性质相关的各种问题,并使自己无法使用大量的DataTables API方法来捕获错误或丰富信息。您的应用具有新功能。

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

相关问题 首次点击事件后,on on click事件不起作用。 - on click event not working after first on click event. mootools触发点击事件。 如何传递自己的数据? - mootools fire click event. How to pass own data? 如何在点击事件中刷新标签内容。 WordPress和Javascript - How to refresh tab content on click event. Wordpress and Javascript 通过单击事件将背景图像添加到div中。 - Adding background image to div with click event. React应用程序-IconMenu在Click事件上未展开。 - React app - IconMenu not expanded on Click event. 多个选择器在jQuery click事件中不起作用。 我用过merge()。 它适用于前两个选择器 - Multiple selectors not working in jQuery click event. I've used merge(). It works for first two selector 如何在动态创建的按钮的click事件上执行javascript函数。 - how to execute javascript function on dynamically created button's click event. 如何在click事件上将函数值传递到文本框中。 函数值也是数组的输出 - How to pass a function value into a text box on click event. Function value is also an output of an array 在锚点html元素上按空格键不会触发click事件。 如何使这种情况发生? - Spacebar key press on anchor html element doesn't trigger click event. How to let this happen? 如何在更改事件中使用。 而不是 onkeyup - How To Use on change event. Instead of onkeyup
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM