我看来不可能实现这个数组结构
[{ "Field1" : "Foo"}, {"Field2" : "Bar" }]
使用以下代码
var matching = new Array();
$('tr[type="entity"]').each(function(){
var $select = $(this).find('select');
matching[$select.attr('id')] = $select.val();
});
这个
alert(JSON.stringify(matching))
始终返回 [ ]。如果是 php,我的数组就会像这样
$matching = array(
"Field1" => "Foo",
"Field2" => "Bar"
);
我知道javascript中没有关联数组。但是我如何根据我的代码实现这样的数组。
最佳答案
对象是关联数组。考虑使用var matching = new Object();
相反,并检查以确保该函数确实正在运行(即您期望的 tr
实际上存在)。
关于Javascript 'associative' 数组疯狂,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14297422/