0

これは私が支援を求めるのは初めてのことです (まだ少し初心者のコーダーです)。

何千ものアイテムを持つ JSON ファイルを作成しました。そのファイルからさまざまなページにドロップダウンを動的に入力し、後で以前の選択に基づいてドロップダウンを動的に入力できるようにしたいと考えています。私はこれを徹底的に調査しましたが、私が見つけたほとんどのチュートリアルと質問は、この特定の方法では機能しないようです. 基本的に、ページをロードすると、アイテムが表示されません。つまり、ドロップダウンは空です。コンソールにはエラーが 1 つだけ表示されます。これは、見つけたすべての情報によると、CDN が読み込まれていないことを示しています。だから、ここにあります:

    <html>
  <head>
    <script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
  </head>
   <body>
     <select id="mainhand"></select>
     <br>
     <select id="offhand"></select>

<script type="text/javascript">
    $(document).ready(function() {
                //request the JSON data and parse into the select element
                $.getJSON('http://nodtools.net/stats.json', function(obj) {

                            //iterate over the data and append a select option
                            $.each(data.item, function(key, value) {
                                    // set the value of option to each item in the list as you iterate through
                                    var option = $('<option><option/>').val(value.name).text(value.name);
                                    // almost any of them can be added to the main hand
                                    if (value.type == 'slash' || value.type == 'crush' || value.type == 'pierce' || value.type == 'whip' || value.type == 'staff') {
                                        $('#mainhand').append(option);
                                    };
                                    // only shields and 1h weapons can be in off hand
                                    if (value.hands == 1 || value.type == 'shield') {
                                        $('#offhand').append(option);



                                        // continue on for all types: bow, arrow, quiver, etc.
                                        });
                                    });

                            });
                        });
</script>
</body>

</html>

明らかに何かが欠けていると強く感じています...誰かがそれを指摘できれば、私はそれを大いに感謝します!

4

2 に答える 2

0

Alex Berd から提供された JSFiddle を更新しました。

両方を (正確に) 同じ$.each(/**/)ブロックに入力することはできませんが、その理由を明確に説明することはできません。他の誰かがそれをクリアできると確信しています。

コードの関連部分は次のようになります。

$(document).ready(function () {
   //iterate over the data and append a select option
    $.each(items, function (key, value) {
    // set the value of option to each item in the list as you iterate through
    var option = $('<option>').val(this.name).text(this.name);
    // almost any of them can be added to the main hand
    if (items.type != 'shield' && items.type != 'bow' /* etc. */) {
        $('#mainhand').append(option);
        }
    })

    //iterate over the data and append a select option
    $.each(items, function (key, value) {
        // set the value of option to each item in the list as you iterate through
        var option = $('<option>').val(this.name).text(this.name);
        // only shields and 1h weapons can be in off hand
        if (this.hands == 1 || this.type == 'shield') {
            $('#offhand').append(option);
        }
    })
    /*
     * continue on for all types: bow, arrow, quiver, etc.
     */
})

あとは、.json データを適切にインポートするだけです。

ありがとう、

于 2015-10-22T23:41:49.370 に答える
0

括弧が正しい場所にある、または括弧が不要であるなど、コードに間違いがあると思います。

$(document).ready(function() {
                //request the JSON data and parse into the select element
                $.getJSON('http://nodtools.net/stats.json', function(obj) {

                            //iterate over the data and append a select option
                            $.each(data.item, function(key, value) {
                                    // set the value of option to each item in the list as you iterate through
                                    var option = $('<option><option/>').val(value.name).text(value.name);
                                    // almost any of them can be added to the main hand
                                    if (value.type == 'slash' || 
                                        value.type == 'crush' ||
                                        value.type == 'pierce' ||
                                        value.type == 'whip' ||
                                        value.type == 'staff') 
                                    {
                                        $('#mainhand').append(option);
                                    }
                                    // only shields and 1h weapons can be in off hand
                                    if (value.hands == 1 || value.type == 'shield') {
                                        $('#offhand').append(option);
                                    }

                                        // continue on for all types: bow, arrow, quiver, etc.
                                        });
                                    });

                            });

このJSFIDDLEを試してみ てください。

于 2015-10-22T05:27:41.490 に答える