如何在http帖子上执行.load()?

时间:2013-04-01 15:25:33

标签: javascript jquery html ajax

在我的应用程序中,我有一段代码可以“捕获”锚点上的所有点击,并将其加载到我的页面中:

$("a").live("click", function () {
    var src = $(this).attr("href");
    if ($(this).attr("target") === "_blank")
        return true;
    $("#myPage").load(src + " #myPage");
    return false;
});

现在这适用于所有锚标签。如何使我的所有POST请求(发送表单数据)都像这样?

编辑:正如凯文所说,我尝试使用.post,但它对我不起作用,我做错了什么?这是代码:

$("form").post("submit", function () {
    var src = $(this).attr("action");
    $("#myPage").load(src + " #myPage");
    return false;
});

1 个答案:

答案 0 :(得分:1)

使用表单提交事件来处理表单POST案例

$(document).on('submit', 'form', function(){
    var $this = $(this);

    $.ajax({
        url: $this.attr('action'),
        type: 'POST',
        data: $this.serialize()
    }).done(function(responseText){
        $("#myPage").html(responseText)
    });

    return false;
})

由于您使用的是jquery 1.9,因此可能需要将a处理程序重写为

$(document).on("click", 'a', function () {
    var src = $(this).attr("href");
    if ($(this).attr("target") === "_blank")
        return true;
    $("#myPage").load(src + " #myPage");
    return false;
});
相关问题