2012-04-26 46 views
0

我有一个菜单。这些菜单包括一些艺术家,当管理员点击复选框时,艺术家将被插入到数据库中。将值附加到div作为数组 - jquery

首先,

function AddDataImdb(par, par2, par3) { 

    var artistIds = []; 
    $("input[name='" + par + "']:checked").each(function() { 
     artistIds.push($(this).val()); 
    }); 

    $.post('/json/management/AddDataImdbAjax', { 
     "artistIds": artistIds 
    }, function (response) { 
     if (response == 'error') { 

      alert("Sanatçı bulunamadı, yönlendiriliyorsunuz"); 
      window.location.replace("myurl"); 

     } else { 
      alert("succesfully added"); 
      $("#" + par2 + "").append(('<br /><input type="checkbox" name =' + par + ' value=' + response + ' />' + par3 + '<br />')); 
     } 
    }); 

} 

有了上面的代码,我可以加我的div新增加的艺术家。

的问题是,当管理员插入2个或更多的艺术家,它显示了最后一个为增加

$("#"+par2+"").append(('<br /><input type="checkbox" name ='+par+' value='+response+' />'+par3+'<br />')); 

上面的代码中添加格。我怎样才能将这些添加为数组?我能做什么 ?

+0

你是什么'response'?它不显示最后一个div,我认为它只附加了一个div。要添加多个div,你应该循环你的'响应' – thecodeparadox 2012-04-26 17:42:47

+0

我不清楚你想要发生什么。如果响应中返回多个值,是否需要创建新的复选框? “响应”格式如何? – veeTrain 2012-04-26 17:43:33

回答

1

你能说出更多?回应的内容是什么?

如果你的反应是一个列表,那么你可以这样做:

$.each(reponse, function() { 
    //append as div 
}): 
+0

var myArr = response;我把我的JSON这样我可以用这个像foreach在PHP? – snnlankrdsm 2012-04-26 18:18:35

+0

尝试使用$ .each(reponse,function(index,value){}); OR $ .map(response,function(value,key){}); – chungwufei 2012-04-26 18:37:07