开发者

jQuery ajaxStart & ajaxComplete live?

开发者 https://www.devze.com 2023-01-26 09:03 出处:网络
I have the following working just fine: $(\".new_comment\").ajaxStart(function() { ajaxBanner(\'show\');

I have the following working just fine:

$(".new_comment").ajaxStart(function() {
    ajaxBanner('show');
    // Disable button
    $("input[type=submit]", this).attr("disabled", true);
});
$(".new_comment").ajaxComplete(function() {
    ajaxBanner('hide');
    // Re-enable button
    $("input[type=submit]", this).removeAttr("disabled");
});

Proble开发者_Python百科m is that only works if the page renders with the comment form. In many cases the comment form is dynamically injected on the page, and this then fails.

How can I make the above live()?

thanks


This isn't tested, but perhaps worth a try?

$(".new_comment").live("ajaxComplete", function() {
    ajaxBanner('show');
    // Disable button
    $("input[type=submit]", this).attr("disabled", true);
});

The alternative is to add the event when you dynamically inject the comment form, but we'll need some more of your code for that (i.e. where the injection happens).


I'm not sure exactly how .ajaxComplete() works when binding to a jQuery selection, but I reckon you could solve your problem easily by binding to an element you know exists, i.e. document, and then using normal selectors:

$(document).ajaxStart(function() {
    ajaxBanner('show');
    // Disable button
    $(".new_comment input[type=submit]").attr("disabled", true);
}).ajaxComplete(function() {
    ajaxBanner('hide');
    // Re-enable button
    $(".new_comment input[type=submit]").removeAttr("disabled");
});
0

精彩评论

暂无评论...
验证码 换一张
取 消