2014-10-01 66 views
0

目标:通过它在jquery的创建二维阵列和环

  1. 通过每个键在JavaScript中创建/ jquery的两个维阵列
  2. 推送数据到它
  3. 循环,值对
  4. 呼叫在循环功能

代码:

var IDs = []; 
    /* Find Input elements and push its ID & Value into an array */ 
    $('#divDynamicFields').find("input").each(function() { 
     IDs.push(this.id, $(this).val()); 
    }); 
    console.log(IDs); /* Now it prints string seprated by ',' */ 

    /* Loop Through Each element in 2D array */ 
    $.each(IDs, function(key, value) { 
    $.each(key, function(innerKey, innerValue){ 
     CallFunction(id,val); 
     /* Will This Work ? */ 
     } 
    } 
+2

我在这个1维数组中看不到2个维度。所以我想答案是不可行的,那是行不通的。 – Spokey 2014-10-01 11:50:48

+0

@Spokey我想创建2d数组,其中包含'id,value'对 – Shaggy 2014-10-01 11:52:25

+1

看看[fiddle](http://jsfiddle.net/2wyoe27w/)。 – Regent 2014-10-01 11:56:42

回答

9

的整体思路是推到阵列不是两个元素,但一个阵列,它由两个部分组成:

JSFiddle

var IDs = []; 
$('#divDynamicFields input').each(function() 
{ 
    IDs.push([this.id, $(this).val()]); 
}); 

for (var i = 0; i < IDs.length; i++) 
{ 
    CallFunction(IDs[i][0], IDs[i][1]); 
} 

function CallFunction(id, value) 
{ 
    console.log("ID: " + id + ", value: " + value); 
} 
1

插入

var IDs = {}; 
$('#divDynamicFields').find("input").each(function() {  
    IDs[this.id]= $(this).val(); 
}); 

而且同样环

$.each(IDs , function (index, value) { 
    alert(index + ' : ' + value); 
}); 
+1

什么是'news'和'obj'? 'obj。[this.id]' - 它应该是'obj [this.id]',不是吗? '$('#divDynamicFields')。find(“input”)''会看起来好一点'$('#divDynamicFields input')'。 – Regent 2014-10-01 11:59:27

+0

@Regent,它的错误错误bro,逻辑很重要。谢谢修正answer.DOWNVOTER关心评论?答案是正确的? – 2014-10-01 12:02:04

+0

人类可以理解错字'obj。[this.id]'=>'ID。[this.id]'这难吗? – 2014-10-01 12:08:55

0

你推语法和迭代使用对象 是错误的。你应该这样做:

var IDs = []; 
 

 
IDs.push([1, 2]); 
 
IDs.push([2, 3]); 
 

 
/* Loop Through Each element in 2D array */ 
 
$.each(IDs, function(key, value) { 
 
    alert(value); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

1

你有几个问题..

第一个是,你必须输入添加为一个数组

 IDs.push([this.id, $(this).val()]); 

第二个是,你要拨打的你刚刚加在一起的ID,你不想做一个双循环。

$.each(IDs, function(key, value) { 
     CallFunction(value[0],value[1]); 
}); 

这是一个例子:

var IDs = []; 
/* Find Input elements and push its ID & Value into an array */ 
$('#divDynamicFields').find("input").each(function() { 
    IDs.push([this.id, $(this).val()]); 
}); 
console.log(IDs); /* Now it prints string seprated by ',' */ 

/* Loop Through Each element in 2D array */ 
$.each(IDs, function(key, value) { 
     CallFunction(value[0],value[1]); 
}); 

function CallFunction(id,val) { 
    console.log(id+","+val); 
} 

JSFiddle

-4

var IDs = []; 
 

 
IDs.push([1, 2]); 
 
IDs.push([2, 3]); 
 

 
/* Loop Through Each element in 2D array */ 
 
$.each(IDs, function(key, value) { 
 
    alert(value); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

0

你可以试试这个用

var IDs = []; 
$('#divDynamicFields').find("input").each(function() { 
    IDs.push({ 
     id: $(this).val()   
    }); 
}); 

console.log(IDs); 

创建jQuery中一维数组,这将允许你发送任何你想要的阿贾克斯阵来传递您的所有数据。