jQuery。通过ajax格式化和发送的最佳实践

时间:2015-07-13 20:34:15

标签: javascript jquery ajax

运行.each的最佳做法是什么,并通过ajax将值作为数组发送。

 $( ".sortable li" ).each(function( index ) {
      console.log($(this).data("trackid"));
 });

这将在控制台窗口11,2,45,23,4,31中输出。

1 个答案:

答案 0 :(得分:0)

Well you could use the push method to add the id's to an array. I've posted an array like this to PHP and it works well.

 var theArray = [];
 $( ".sortable li" ).each(function( index ) {
     theArray.push($(this).data("trackid"));
 });

 // Send via Ajax
 $.post(url,{ids:theArray},function(response){
     // Code to handle response
 });