我有一个函数可以显示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);
})
})
})
答案 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);
})
})
})