我想動態地創建一個JSON數組。在該網站中,有一個動態編號爲<的div id =「#selected」>,我需要獲取所有值並創建一個JSON數組。動態創建JSON數組與.each()
我遇到了.push()的功能,但我一直無法弄清楚。的
<!-- there could be a million of these, or only one... each value is unique though -->
<div id="selected" value="5|3"></div>
<div id="selected" value="3|65"></div>
function json_array_selected() {
var JSon = {};
$('div#selected').each(function() {
// let's first split the given values
var Split = $(this).attr('value');
Split = Split.split('|');
var Type = Split[0];
Value = Split[1];
// now let's set up our Json array... using the value = type way, there should never be
// any repeating
JSon.Value = Type;
});
return JSon;
}
ID應該是唯一的,以便'$( 'DIV#選擇' )'只會給你一個元素 – Musa 2012-07-09 22:33:44
,即使我用.each()做循環? – 2012-07-09 22:39:14
其實它工作http://jsfiddle.net/Ldm7U/,但ID應仍然是唯一的 – Musa 2012-07-09 22:43:49