在ajax调用后获取自动完成选择器

时间:2012-07-23 15:56:46

标签: jquery

我正在尝试在进行ajax调用后在“success”中找到jquery自动完成的选择器。在“成功”中,我正在寻找$(this)为$(“。itemDescInput”),但事实并非如此。那时我如何找到那个选择器呢?

$(".itemDescInput").autocomplete({
    source: "invoices/invoice_items.php",
    select: function(event, ui) {
        var $it_desc = ui.item.value;
        $.ajax({
            type: "POST",
            url: "invoices/invoice_items_prices.php",
            data: "it_desc="+$it_desc,
            success: function(result){
                if(result != '') {
                    $(this).find("input .itemCostEach").val(result);
                }
            }
        });
    }
}); 

1 个答案:

答案 0 :(得分:2)

success()内,this不是指您的选择器,而是指 AJAX设置。您可以通过在this来电中设置上下文来更改ajax()指向的内容。请参阅documentation

例如:

    $.ajax({
        context: this,  // You could pass 'this' in directly if you wanted :)
        success: function(result) {
            // Refer to 'this' in the way you expect
        }
    });