简体   繁体   English

Ajax发布完成后触发点击事件

[英]Trigger click event once Ajax post is complete

I am trying to trigger a click event once and Ajax post is complete. 我试图触发一次click事件,并且Ajax发布完成。 However, it is not firing because the element is not available in the DOM. 但是,由于元素在DOM中不可用,因此无法触发。 I call a jQuery .load method in the success callback of the Ajax post to load the new DOM element. 我在Ajax帖子的success回调中调用jQuery .load方法来加载新的DOM元素。

Ajax post: Ajax发布:

$.ajax({
    type: "POST",
    url: $formAction,
    data: $form.serialize(),
    success: function (result) {
        if (result.constructor === Array) {
            $categoryValidationPanel.html('');
            $.each(result, function (index, value) {
                $categoryValidationPanel.append("<li>" + result[index].ErrorMessage + "</li>")
            });
        } else {
            $dialog.modal('hide');
            refreshComplaintCategoryResults(); //Call to load method below
        }
    },
    error: function () {
        alert("There was a problem processing your request, please try again.");
    }
}).done(function () {
    $("tr[data-action-url='/Admin/ComplaintWorkflow/Edit?categoryId=" + $categoryId + "']").trigger("click");
});

Load method: 加载方式:

function refreshComplaintCategoryResults() {
    $(".complaint-category-results").load('ComplaintWorkflow/GetComplaintCategoryResults');
}

I would expect the order of execution to be: 我希望执行的顺序是:

  1. Ajax post 阿贾克斯邮报
  2. On success load newly created element into DOM via refreshComplaintCategoryResults() 成功完成后,通过refreshComplaintCategoryResults()新创建的元素加载到DOM中
  3. do .done and trigger click on newly created DOM element .done并触发单击新创建的DOM元素

However, this is not the case, the .load method call is in pending state until after the .done is executed and hence the DOM is not updated. 但是,情况并非如此, .load方法调用处于挂起状态,直到执行完.done之后,因此DOM才更新。

I have also tried this in the .load method (passing the categoryId from the ajax post): 我也曾在.load方法中尝试过此操作(从ajax帖子传递categoryId):

function refreshComplaintCategoryResults(categoryId) {
    $(".complaint-category-results").load('ComplaintWorkflow/GetComplaintCategoryResults', function () {
        $("tr[data-action-url='/Admin/ComplaintWorkflow/Edit?categoryId=" + categoryId + "']").trigger("click");
    });
}

Can anyone suggest what I am missing here? 谁能建议我在这里缺少什么?

UPDATE: 更新:

I have now got newly created element in the DOM and then the relevant trigger code being executed, however, for some reason it does not execute. 现在,我在DOM中获得了新创建的元素,然后正在执行相关的触发代码,但是由于某种原因,它无法执行。 I updated the load method to (thanks @TrueBlueAussie for his comments on this): 我将加载方法更新为(感谢@TrueBlueAussie对此的评论):

function refreshComplaintCategoryResults(categoryId) {
    $.get("ComplaintWorkflow/GetComplaintCategoryResults", function (result) {
        $(".complaint-category-results").html('');
        $(".complaint-category-results").html(result);
    })
    .done(function () {
        $("tr[data-action-url='/Admin/ComplaintWorkflow/Edit?categoryId=" + categoryId + "']").trigger("click");
    });
}

The trigger code is not working. 触发代码不起作用。 I have debugged in the console and the selector definitely works. 我已经在控制台中调试了,选择器肯定可以工作。 If I let the code run through and then execute the same code in the console, the trigger works. 如果我让代码运行完毕,然后在控制台中执行相同的代码,则触发器起作用。

Any ideas? 有任何想法吗?

Using jquery deferred objects is the best solution here. 在这里使用jquery延迟对象是最好的解决方案。 You can chain the callback as required after the completion of ajax request.Here is a link about how to use jquery deferred objects - http://api.jquery.com/category/deferred-object/ 您可以在ajax请求完成后根据需要链接回调。以下是有关如何使用jquery延迟对象的链接-http: //api.jquery.com/category/deferred-object/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM