Reputation: 15027
Let's say I have this code which gets the latest posts via ajax:
$.ajax({
url: loadmore,
data: {lastid: lastid,mode:'latest'},
dataType: 'json',
type: 'POST',
timeout: 10000,
success: function(json){
//some code
},
error: function(jqXHR, textStatus, errorThrown){
//some code
}
});
How do I change the content of the data? Here's my try but something failed.
$.ajax({
url: loadmore,
if($('.postlist').hasClass('best'))
data: {lastrank: lastrank,mode: 'best'},
else
data: {lastid: lastid,mode:'latest'},
dataType: 'json',
type: 'POST',
timeout: 10000,
success: function(json){
//some code
},
error: function(jqXHR, textStatus, errorThrown){
//some code
}
});
Upvotes: 0
Views: 2822
Reputation: 13560
You could use the ?: (ternary operator) operator:
data: ($('.postlist').hasClass('best')) ?
{lastrank: lastrank,mode: 'best'} :
{lastid: lastid,mode:'latest'},
Upvotes: 4
Reputation: 3828
Ternary operator:
$.ajax({
url: loadmore,
data: ($('.postlist').hasClass('best') ?
{lastrank: lastrank,mode: 'best'} :
{lastid: lastid,mode:'latest'}),
dataType: 'json',
type: 'POST',
timeout: 10000,
success: function(json){
//some code
},
error: function(jqXHR, textStatus, errorThrown){
//some code
}
});
Upvotes: 5