简体   繁体   English

在数据表行的按钮单击上调用javascript

[英]invoke javascript on button click of a datatable row

Im using dataTable and I have the following: 我正在使用dataTable并且有以下内容:

 var oTable = $('#example').dataTable({

                bProcessing: true,
                sAjaxSource: "/Cart/addresses",
                'iDisplayLength': 5,
                aaSorting: [],
                aoColumns: [
                    { "mData": "Contact" },
                    { "mData": "Address" },
                    { "mData": "Postcode" },
                    {
                        "mData": null,
                        "mRender": function (data, type, full) {
                            return '<button type="button" class="btn btn-success use-address" onclick="doFunction();" role="button" data-adcode="' + full.ADCode + '">' + 'Use Address' + '</button>';
                        }
                    }

                ]
            });

I have tried the following, i basically want to be bale to use adcode when the button is clicked: 我已经尝试了以下方法,基本上我想当点击按钮时使用adcode:

 $('.use-address').click(function () {
        var adccode = $(this).data("adcode");

    });

    function doFunction()
    {
        var adccode = $(this).data("adcode");

    }

This wont work, in firebug it doesnt hit either of these. 这是行不通的,在萤火虫中它没有命中任何一个。

Note the datatable is added dynamically (after clicking a button.) 请注意,数据表是动态添加的(单击按钮后)。

Any ideas why it aint working? 任何想法为什么它不起作用?

Thanks 谢谢

Try to use event delegation here: 尝试在此处使用事件委托

$('#example').on('click','.use-address',function () {
    var adccode = $(this).data("adcode");
});

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

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