var item = {
"ID": "01",
"sizes" : {
"Height": { "2XS": 66,"XS": 68,"S": 70,"M": 72,"L": 74,"XL": 76,"2XL": 78,"3XL": 80,"4XL": 82,"5XL": ""},
"Width under arm": { "2XS": 46,"XS": 68,"S": 70,"M": 72,"L": 74,"XL": 76,"2XL": 78,"3XL": 80,"4XL": 82,"5XL": ""},
"Bottom width": { "2XS": 46,"XS": 68,"S": 70,"M": 72,"L": 74,"XL": 76,"2XL": 78,"3XL": 80,"4XL": 82,"5XL": ""},
"Neck openeing": { "2XS": 46,"XS": 68,"S": 70,"M": 72,"L": 74,"XL": 76,"2XL": 78,"3XL": 80,"4XL": 82,"5XL": ""},
"Arm openeing": { "2XS": 18,"XS": 68,"S": 70,"M": 72,"L": 74,"XL": 76,"2XL": 78,"3XL": 80,"4XL": 82,"5XL": ""} // no comma
}
}
/* the above could be
$.get("items.php?id=01",function(data) {
var item = JSON.parse(data); // not necessary to parse if the server returns aplicate/json
});
*/
var names = {
"2XS": "XX Small",
"XS": "X Small",
"S": "Small",
"M": "Medium",
"L": "Large",
"XL": "X large",
"2XL": "XX Large",
"3XL": "XXX Large",
"4XL": "XXXX Large",
"5XL": "XXXXX Large"
}
$(function() {
var $sel = $("#sel1"); // cache the object for reuse
$.each(item.sizes.Height, function(key, val) { // create the select from the object above - I use the Height list of sizes
if (val) { // ignore empty sizes
$sel.append($('<option/>', {
"value": key,
"text": names[key]
}));
}
});
$sel.on("change", function() {
process(this.value); // pass select value to process
}).change(); // trigger the current selection
});
function process(aSize) { // passes XS ... 5XL
var $table = $("#sizeTable").empty();
$.each(item.sizes, function(key, val) { // loop over height, width,
$table.append('<tr><td class="nameCell">' + key + '</td><td class="sizeCell">' + val[aSize] + ' cm</td></tr>')
});
}
.nameCell {
font-weight: bold;
padding-right: 5px;
}
.sizeCell {
font-style: italic;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="sel1"></select>
<table>
<tbody id="sizeTable"></tbody>
</table>
あなたは出力があることを期待するものを追加することはできますか?あなたの質問から伝えるのは難しいです。 –
JSONカスケードドロップダウンを検索してくださいPHP AJAX - ブラウザにJSONを送信してオブジェクトを処理させることができます – mplungjan
データが表示されたらすぐにこれを調べます! – user1296114