如何在api函数中找到最接近的tr

时间:2016-03-23 05:44:55

标签: jquery

我有一个函数可以显示api函数内最接近tr的数据。问题是他们无法找到最接近的tr。这是我的代码:

$('.pricing_level').on('change', function(e){
    var pricing_level_id = e.target.value;
    $('tbody.subcontent tr').each(function(){
        var item_id =  $(this).closest('tr').find(".details_itemid").val();

        var category_id = $(this).closest('tr').find(".details_category_id").val();
        var subcategory_id = $(this).closest('tr').find(".details_subcategory_id").val();
        var net_unit_price = $(this).closest('tr').find(".details_unitprice").val();

        $.get('/dev/api/itempricinglevel?item_id=' + item_id + '&pricinglevel_id=' + pricing_level_id, function(itempricinglevel){

          $(this).closest('tr').find(".details_unitprice").val(itempricinglevel.min);
        })
    }) 
})

1 个答案:

答案 0 :(得分:1)

在进行ajax调用之前将this变量分配给另一个变量,因为在ajax中,this变量会被Ajax调用的jqXHR对象覆盖。

$('.pricing_level').on('change', function(e){
    var pricing_level_id = e.target.value;
    $('tbody.subcontent tr').each(function(){
        var item_id =  $(this).closest('tr').find(".details_itemid").val();

        var category_id = $(this).closest('tr').find(".details_category_id").val();
        var subcategory_id = $(this).closest('tr').find(".details_subcategory_id").val();
        var net_unit_price = $(this).closest('tr').find(".details_unitprice").val();
        var that = this;

        $.get('/dev/api/itempricinglevel?item_id=' + item_id + '&pricinglevel_id=' + pricing_level_id, function(itempricinglevel){
            $(that).closest('tr').find(".details_unitprice").val(itempricinglevel.min);
        })
    }) 
})