为什么jquery。 On()不适用于动态元素?

时间:2013-07-28 09:34:32

标签: jquery event-handling kendo-ui kendo-grid

我希望有权通过$ .ajax()在我点击更新按钮的时候发送我的信息,但是我无法处理更新按钮点击事件。

我希望代码能够处理更新按钮点击事件但我没有工作

jsfiddle code

代码:

 $(".table").on("click", "k-grid-update", (function () {

        alert('xxx');

        //$.ajax({

        //    url: 'api/apdevice',
        //    type: 'PUT',
        //    datatype: 'application/json',
        //    data: {},
        //    success: function (data) {

        //    },

        //    error: function (data) {

        //    }

        //});

谢谢

2 个答案:

答案 0 :(得分:1)

$(".table").on("click", "k-grid-update",

有你的问题。您很可能需要委托的类选择器,因为不存在名为k-grid-update的HTML标记:

$(".table").on("click", ".k-grid-update",

答案 1 :(得分:1)

并不是它不起作用:问题是KendoUI重新定义了同一元素的动作。

您不应该直接将其绑定到按钮,而是使用一些KendoUI提供的机制来执行此操作。要么在Kendo Grid中使用save事件,要么在DataSource.transport上定义一个方便的update函数。

示例:

$('.table').kendoGrid({
    dataSource : {
        transport: {
            read  : function (op) {
                op.success(data)
            },
            update: function (op) {
                alert("xxx - update");
                ...
            }
        },
        schema   : {
            model: {
                id: "Mac"
            }
        }
    },
    sortable   : true,
    groupable  : true,
    selectable : true,
    navigatable: true,
    height     : 500,
    scrollable : true,
    pageable   : true,

    rowTemplate   : kendo.template($("#client-row-template").html().replace('class="k-alt"', '')),
    altRowTemplate: kendo.template($("#client-row-template").html()),//@class="k-alt"@
    editable      : "popup",
    save          : function (a) {
        alert("xxx - save");
        ...
    }

});