2012-03-26 47 views
0

我有4个功能,做我觉得可以做的事情。无法清理我的代码

function getDownline(rankid, args, type) { 
    $('body').append('<div class="modal">'); 
    $.getJSON('message_center_getMessageLists.asp', args, function (json) { 
     var options = ''; 
     $.each(json.downline, function (i, item) { 
      options += '<option value="' + item.distID + '" title="' + item.name.toLowerCase() + ' (' + item.distID + ')" >' + item.name.toLowerCase() + ' (' + item.distID + ')</option>'; 
     }); 

     $('#fromList').find('option') 
      .remove() 
      .end(); 

     $('#fromList').append(options); 
    }) 
    .success(function() { $('.modal').remove(); }) 
    .error(function() { 
     alert("There was an error while trying to make this request; If it persists please contact support"); 
     $('.modal').remove(); 
    }); 
} 

唯一真正的区别是json.downline它可以是4个不同的东西。我添加了type参数。我试图用来传递downline, sponsor, pSponsor or monitorlist

但我似乎无法只做json.typejson. + type任何想法我失踪?

回答

1

json[type]将获得由type命名的属性。

+0

这样做的伎俩,谢谢。 – 2012-03-26 17:32:12