2012-02-18 6 views
0

Скажем, у меня есть этот код, который получает последние сообщения через AJAX:если/другое заявление в .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 
    } 
    }); 

Как изменить содержание данных? Вот моя попытка, но что-то не получилось.

$.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 
    } 
    }); 

ответ

5

троичный оператор:

$.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 
    } 
    }); 
4

Вы могли бы использовать: (тройная оператор) оператор:

data: ($('.postlist').hasClass('best')) ? 
      {lastrank: lastrank,mode: 'best'} : 
      {lastid: lastid,mode:'latest'},