JSONファイルから、選択した内容に応じて、各ループごとにドロップダウンボックスからデータを取得しようとしています。私は自分のコードをこのようにしたい:ドロップダウンボックスで値の年をクリックすると、選択された同じ年のファイルからすべてのオブジェクトの値がテーブルに表示されます。JSONファイルからHTMLにデータを挿入
<!DOCTYPE html>
<html>
<body>
<form>Select a YEAR :
<select id="Selections">
<option value="year1">year1</option>
<option value="year2">year2</option>
</select>
</form>
<button type="button" onclick="run()">Try it</button>
<p id="result"> </p>
<script>
function run() {
var x = document.getElementById("Selections").value;
if (x == "year1") {
document.getElementById("result").innerHTML = "the first year";
$.getJSON("year1.json", function(json) {
$.each(json, function (i,item){
$('#tblinfo').append('<tr> <th>'+item.year+'</th>
<th>'+item.eposide+'</th></tr>');
})
});
} else if (x == "year2") {
document.getElementById("result").innerHTML = "the second year";
$.getJSON("year2.json", function (json) {
$.each(json, function (i,item){
$('#tblinfo').append('<tr> <th>'+item.year+'</th>
<th>'+item.eposide+'</th></tr>');
})
});
}
}
</script>
<table id="tblinfo" border=" 1px solid black" style="width:100%"></table>
</body>
</html>
私のJSONファイル:ここで
は私のコードです
year1.jsonは次のとおりです。
[
{"year":"1995", "eposide":"1"},
{"year":"1995", "eposide":"2"},
{"year":"1995","eposide":"3"}
]
year2.jsonは次のとおりです。
[
{"year":"1994", "eposide":"1"},
{"year":"1994", "eposide":"2"},
{"year":"1994","eposide":"3"}
]
なんだかテーブルの値を取得できません。
テンプレートリテラルを使用しない限り、あなたは空白行を持つことができない - 。このニーズを一行であることを '$( '#のtblinfoは、')( '
ブラウザコンソールにスローされたエラーはありますか?構文上の問題がありますが、コピー/ペーストエラーの可能性があります – charlietfl
エラーメッセージが表示されますか? '#result' HTMLの変更が見えますか? – showdev