更新到Jquery 1.9.0时jquery.unobtrusive-ajax插件损坏 [英] jquery.unobtrusive-ajax plugin broken when updating to Jquery 1.9.0

查看:127
本文介绍了更新到Jquery 1.9.0时jquery.unobtrusive-ajax插件损坏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
jQuery 1.7-将live()转换为on()

Possible Duplicate:
jQuery 1.7 - Turning live() into on()

//解决方案:我简单地替换了四处出现的问题,例如建议的已批准答案,并且不引人注目的ajax插件已启动并可以与jquery 1.9.0一起使用

//Solution: I simply replaced the four occurrences like the approved answer suggested and unobtrusive ajax plugin is up and working again with jquery 1.9.0

更新//观察注释在底部的答案,这是解决此问题的最佳方法.

Update//Observe the comments for the answer marked at the bottom which is the best way to solve this.

//原始帖子: 我升级到jQuery 1.9.0,但随后不引人注目的ajax插件出现故障,因为它们不赞成使用live方法.我尝试这样替换它,因为升级为我修复了另一个错误.但是,它不起作用.我简单地将live替换为on:

//Original post: I upgraded to jQuery 1.9.0 but then unobtrusive ajax plugin went down since they deprecated the live method. I tried to replace it like this since the upgrade fixes an other bug for me. However, it does not work. I simply replaced live with on like this:

$("a[data-ajax=true]").on("click", function (evt) {
        evt.preventDefault();
        asyncRequest(this, {
            url: this.href,
            type: "GET",
            data: []
        });
    });

    $("form[data-ajax=true] input[type=image]").on("click", function (evt) {
        var name = evt.target.name,
            $target = $(evt.target),
            form = $target.parents("form")[0],
            offset = $target.offset();

        $(form).data(data_click, [
            { name: name + ".x", value: Math.round(evt.pageX - offset.left) },
            { name: name + ".y", value: Math.round(evt.pageY - offset.top) }
        ]);

        setTimeout(function () {
            $(form).removeData(data_click);
        }, 0);
    });

    $("form[data-ajax=true] :submit").on("click", function (evt) {
        var name = evt.target.name,
            form = $(evt.target).parents("form")[0];

        $(form).data(data_click, name ? [{ name: name, value: evt.target.value }] : []);

        setTimeout(function () {
            $(form).removeData(data_click);
        }, 0);
    });

    $("form[data-ajax=true]").on("submit", function (evt) {
        var clickInfo = $(this).data(data_click) || [];
        evt.preventDefault();
        if (!validate(this)) {
            return;
        }
        asyncRequest(this, {
            url: this.action,
            type: this.method || "GET",
            data: clickInfo.concat($(this).serializeArray())
        });
    });

推荐答案

使用on(委托)的live等效项是:

Equivalent of live using on (delegation) is:

$(document).on("click","a[data-ajax=true]", function (evt) {...});

您可以在此处找到jquery的.on()方法文档:

You can find jquery's .on() method documentation here:

>> http://api.jquery.com/on/<<

>> http://api.jquery.com/on/ <<

.on()方法将事件处理程序附加到当前选定的集合 jQuery对象中的元素的集合.从jQuery 1.7开始,.on()方法 提供附加事件处理程序所需的所有功能.为了 从旧版jQuery事件方法转换的帮助,请参见 .bind() .delegate() .live().

The .on() method attaches event handlers to the currently selected set of elements in the jQuery object. As of jQuery 1.7, the .on() method provides all functionality required for attaching event handlers. For help in converting from older jQuery event methods, see .bind(), .delegate(), and .live().

要删除与.on()绑定的事件,请参见 .off().要附加仅运行一次然后删除的事件 本身,请参见 .one()

To remove events bound with .on(), see .off(). To attach an event that runs only once and then removes itself, see .one()

这篇关于更新到Jquery 1.9.0时jquery.unobtrusive-ajax插件损坏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆