在ajax成功方法的position
循环中$.each
变量下面的代码中,单击btn_search
时返回undefined。
我要实现的是在checkPosition
循环中将position
函数的值分配给变量$.each
,以便我可以使用变量的值来确定要显示的结果。我该怎么办?
<script type="text/javascript">
var url = "{{URL::route('find.paid.trade.cat') }}";
var position = '';
$(function(){
$('#btn_search').click(function(e){
$("#results_tbl").empty();
var category_id = $('#trade_category').val();
$.ajax({
method:'POST',
url: url,
data : {
category_id : category_id,
},
success:function(response){
$.each(response.result, function(index, value){
position = checkPosition(response.result[index]['id']);
console.log(position);
if(position == 1){
$('#results_tbl').append('<tr><td>'+response.result[index]['business_name']+
'</td><td><button data-id="'+response.result[index]['id']+'" data-cat_id="'+response.result[index]['business_trade_id']+'"'+
'data-subscription_id="'+response.result[index]['active_subscription_id']+'" class="btn_make_1st btn-primary btn">1st Positions</button>'+
'<button data-id="'+response.result[index]['id']+'" class="btn btn_make_2nd btn-info">Make 2nd</button>'+
'<button data-id="'+response.result[index]['id']+'" class="btn btn_make_3rd btn-success">Make 3rd</button></td>'+
'<td></td></tr>')
}
else
{
$('#results_tbl').append('<tr><td>'+response.result[index]['business_name']+
'</td><td><button data-id="'+response.result[index]['id']+'" data-cat_id="'+response.result[index]['business_trade_id']+'"'+
'data-subscription_id="'+response.result[index]['active_subscription_id']+'" class="btn_make_1st btn-primary btn">Make 1st</button>'+
'<button data-id="'+response.result[index]['id']+'" class="btn btn_make_2nd btn-info">Make 2nd</button>'+
'<button data-id="'+response.result[index]['id']+'" class="btn btn_make_3rd btn-success">Make 3rd</button></td>'+
'<td></td></tr>')
}
})
},
error:function(){
}
})
});
});
});
})
function checkPosition(trade_id)
{
var trade_current_position_url = "{{URL::route('find.position') }}";
var current_position ;
$.ajax({
method:'POST',
url:trade_current_position_url,
data:{
trade_id: trade_id
},
success:function(response){
current_position = response.current_pos;
returnPosition(current_position);
},
error:function(){
}
});
}
function returnPosition(position)
{
return position;
}
答案 0 :(得分:1)
checkPosition函数总是返回undefined,因为它本身不返回任何东西,因此从服务器接收到结果后,其ajax函数的答案也将准备就绪。您必须将作品作为函数传递给它:
function checkPosition(trade_id, doIfOk){
var trade_current_position_url = "{{URL::route('find.position') }}";
var current_position ;
$.ajax({
method:'POST',
url:trade_current_position_url,
data:{
trade_id: trade_id
},
success:function(response){
current_position = response.current_pos;
doIfOk(current_position);
},
error:function(){
}
});
现在每个:
checkPosition(response.result[index]['id'], function(position){
console.log(position);
if(position == 1){
$('#results_tbl').append('<tr><td>'+response.result[index]['business_name']+
'</td><td><button data-id="'+response.result[index]['id']+'" data-cat_id="'+response.result[index]['business_trade_id']+'"'+
'data-subscription_id="'+response.result[index]['active_subscription_id']+'" class="btn_make_1st btn-primary btn">1st Positions</button>'+
'<button data-id="'+response.result[index]['id']+'" class="btn btn_make_2nd btn-info">Make 2nd</button>'+
'<button data-id="'+response.result[index]['id']+'" class="btn btn_make_3rd btn-success">Make 3rd</button></td>'+
'<td></td></tr>')
}
else
{
$('#results_tbl').append('<tr><td>'+response.result[index]['business_name']+
'</td><td><button data-id="'+response.result[index]['id']+'" data-cat_id="'+response.result[index]['business_trade_id']+'"'+
'data-subscription_id="'+response.result[index]['active_subscription_id']+'" class="btn_make_1st btn-primary btn">Make 1st</button>'+
'<button data-id="'+response.result[index]['id']+'" class="btn btn_make_2nd btn-info">Make 2nd</button>'+
'<button data-id="'+response.result[index]['id']+'" class="btn btn_make_3rd btn-success">Make 3rd</button></td>'+
'<td></td></tr>')
}
});