使用jQuery添加事件处理程序并不起作用

时间:2015-11-23 09:29:35

标签: javascript jquery

我正在尝试生成一个数据表,在我排了一行之后,附上一个事件处理程序。我开始做一个小测试:

function AddActivityAbstractOnClickEvent(element) {
    $(element).on('click', GetActivityFullDescription(1));
}

function GetActivityFullDescription(id) {
    alert("Click");
}

我在这里称呼它:

function GetActivityAbstracts() {
    $.getJSON("http://localhost:52535/ExampleService.svc/GetTestTableData", function (testData) {
        var object = $.parseJSON(testData);
        var activityTable = $('#activityTable');
        $.each(object, function (index, value) {
            //index here is used to generate unique ids
            var activityId = this['ActivityId'];
            var activityName = this['ActivityName'];
            var activityResponsible = this['Responsible'];
            var activityEstimatedSavings = parseFloat(this['EstimatedSavings']).toFixed(2);
            var activityEstimatedStart = this['EstimatedStart'];
            var activityEstimatedEnd = this['EstimatedEnd'];
            var activityStatus = this['Status'];
            // TODO: Make more user-friendly Status Descriptions instead of C# enum values.
            var tableElement =
                    '<tr>' +
                    '<td id = "activityId_' + index + '" style = "vertical-align: middle; align: center;">'
                    + activityId + '</td>' +
                    '<td style = "vertical-align: middle;">' +
                    '<div class="status-circle" data-toggle="tooltip" data-placement="right"' +
                    'title=" ' + activityStatus + '" style="background-color:' +
                    GetColumnColor(activityStatus) + ';"></div></td>' +
                    '<td id = "activityName_' + index + '" style = "vertical-align: middle;">'
                    + activityName + '</td>' +
                    //Add index for ids here
                    '<td style = "vertical-align: middle;">'
                    + activityResponsible + '</td>' +
                    '<td style = "vertical-align: middle;">'
                    + activityEstimatedSavings + '</td>' +
                    '<td style = "vertical-align: middle;">'
                    + activityEstimatedStart + '</td>' +
                    '<td style = "vertical-align: middle;">'
                    + activityEstimatedEnd + '</td>' +
                    '</tr>';
            var $tableElement = $(tableElement);
            activityTable.append($tableElement);
            AddActivityAbstractOnClickEvent($tableElement);
        });
        $('#current-data-table').append(activityTable);

        /* This call is necessary because the table is added dynamically */
        $('[data-toggle="tooltip"').tooltip();
    });
}

然而,当我访问我的网站时,&#34;点击&#34;在我添加表数据之前添加到表中的每一行都会显示警报,但是当我单击站点上的任何行时没有任何反应。

1 个答案:

答案 0 :(得分:1)

这是一个完整的修正和评论代码。

function GetActivityAbstracts() {
    $.getJSON("http://localhost:52535/ExampleService.svc/GetTestTableData", function (testData) {
        var object = $.parseJSON(testData);
        var activityTable = $('#activityTable');

        // create HTML only by using $.map, it's better for perfs to generate all HTML before adding it into the dom
        var html = $.map(object, function (item, index) {
            //index here is used to generate unique ids
            var activityId = item['ActivityId'];
            var activityName = item['ActivityName'];
            var activityResponsible = item['Responsible'];
            var activityEstimatedSavings = parseFloat(item['EstimatedSavings']).toFixed(2);
            var activityEstimatedStart = item['EstimatedStart'];
            var activityEstimatedEnd = item['EstimatedEnd'];
            var activityStatus = item['Status'];
            // TODO: Make more user-friendly Status Descriptions instead of C# enum values.
            // move id  attr on on <tr> 
            return '<tr id="activityId_' + index + '">' +
                    '<td  style = "vertical-align: middle; align: center;">'
                    + activityId + '</td>' +
                    '<td style = "vertical-align: middle;">' +
                    '<div class="status-circle" data-toggle="tooltip" data-placement="right"' +
                    'title=" ' + activityStatus + '" style="background-color:' +
                    GetColumnColor(activityStatus) + ';"></div></td>' +
                    '<td id = "activityName_' + index + '" style = "vertical-align: middle;">'
                    + activityName + '</td>' +
                        //Add index for ids here
                    '<td style = "vertical-align: middle;">'
                    + activityResponsible + '</td>' +
                    '<td style = "vertical-align: middle;">'
                    + activityEstimatedSavings + '</td>' +
                    '<td style = "vertical-align: middle;">'
                    + activityEstimatedStart + '</td>' +
                    '<td style = "vertical-align: middle;">'
                    + activityEstimatedEnd + '</td>' +
                    '</tr>';

        });
        // Add HTML into activityTable
        activityTable.html(html);
        // add event by using delegate event.
        activityTable.on('click', 'tr', function() {
            GetActivityFullDescription($(this).attr('id'));
        });
        $('#current-data-table').append(activityTable);

        /* This call is necessary because the table is added dynamically */
        $('[data-toggle="tooltip"]').tooltip();
    });
}