0

I am confused why this code is not working:

HTML:

<li><a id="faktura-generate" rel="regular">Faktúra</a></li>
<li><a id="proforma-generate" rel="proforma">Zálohová faktúra</a></li>

JS:

$('#faktura-generate, #proforma-generate').live('click', function () {
    var type = $(this).attr('rel');
    $.ajax({
        url: 'index.php?route=sale/order/superfaktura&token=<?php echo $token; ?>&order_id=<?php echo $order_id; ?>&type=' + type,
        dataType: 'json',
        beforeSend: function () {
            $(this).after('<img src="view/image/loading.gif" class="loading" style="padding-left: 5px;" />');
        }
    });
}); 

I want to show a little loading icon after user clicks one of the link. $("#proforma-faktura").after(...) is working, but $(this).after(...) not.

4

1 回答 1

3

this不是指您传递给“beforeSend”的匿名函数中的元素。

您可以尝试在闭包中捕获它

$('#faktura-generate, #proforma-generate').live('click', function () {
    var that = this,  //  capture 'this' here
        type = $(this).attr('rel');

    // make a function with access to 'that'
    var myFun = function (data) {
        $(that).after('<img src="view/image/loading.gif" class="loading" style="padding-left: 5px;" />');
    };

    $.ajax({
        url: 'index.php?route=sale/order/superfaktura&token=<?php echo $token; ?>&order_id=<?php echo $order_id; ?>&type=' + type,
        dataType: 'json',
        beforeSend: myFun
    });
});

试试看。

也不支持live了,换个方式试试。(不推荐使用的版本:1.7,已删除:1.9)

于 2013-06-26T18:42:54.777 回答