How to get the form or div by name which the AJAX.BeginForm' onsuccess is called

萝らか妹 提交于 2019-12-12 04:58:45

问题


I have called Ajax.BeginForm inside a div element, so I want to get the div element which the form is in. i.e. I want an equivalent of

function function1(event){
var table = $(event.target).parents("table");
}

which can be used from the Ajax.beginform's onsuccess function. Can anyone help here?


回答1:


It turned out that, within the onsuccess function, "this" refers to the form element. So I could easily use,

$(this).find("mydiv");

or

$(this).parent("table").find("mydiv");



回答2:


MVC4 seems to have broken it. Horribly "this" is now the XHR. To workaround, use this nasty hack:

$(function() {

    var lastEvent = null;
    $("body").on("submit", "form[data-ajax=true]", function(e) {

        lastEvent = e;

    });
    window.TagXhrWithEventTarget = function(xhr) {

        xhr.capturedSubmitEvent = lastEvent;

    }

});

and in begin form:

@using (Ajax.BeginForm("MyAction", "MyController", new AjaxOptions
{
    HttpMethod = "POST",
    OnSuccess = "ProcessResponse",
    OnBegin = "TagXhrWithEventTarget"
}

and then in ProcessResponse:

function ProcessResponse(data, status, xhr) {

    var e = xhr.capturedSubmitEvent;
    var control = e.target;
    . . .


来源:https://stackoverflow.com/questions/11756248/how-to-get-the-form-or-div-by-name-which-the-ajax-beginform-onsuccess-is-called

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!