0

在进行 ajax 调用后,我试图在“成功”中找到 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);
                }
            }
        });
    }
}); 
4

1 回答 1

2

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

例如:

    $.ajax({
        context: this,  // You could pass 'this' in directly if you wanted :)
        success: function(result) {
            // Refer to 'this' in the way you expect
        }
    });
于 2012-07-23T16:01:46.490 回答