Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/452.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 映射JSON条目以选择选项_Javascript_Jquery_Json - Fatal编程技术网

Javascript 映射JSON条目以选择选项

Javascript 映射JSON条目以选择选项,javascript,jquery,json,Javascript,Jquery,Json,我有一个简单的宠物: 和HashMap-字符串值是子项的名称。 在html文件中,我得到了Json格式的hashmap,例如: {乔恩:[{姓名:狗,年龄:5},{姓名:猫,年龄:4},{姓名:鹦鹉,年龄:3}],保罗:[{姓名:鹦鹉,年龄:3}]} 在我的html中有两个选择,第一个代表孩子的名字,第二个代表宠物: <select id="Child"> <option value="Jon">Jon</option>

我有一个简单的宠物:

和HashMap-字符串值是子项的名称。 在html文件中,我得到了Json格式的hashmap,例如:

{乔恩:[{姓名:狗,年龄:5},{姓名:猫,年龄:4},{姓名:鹦鹉,年龄:3}],保罗:[{姓名:鹦鹉,年龄:3}]}

在我的html中有两个选择,第一个代表孩子的名字,第二个代表宠物:

       <select id="Child">
           <option value="Jon">Jon</option>
           <option value="Paul">Paul</option>
        </select>
        <select id="Pet">
        </select>

我还没有测试以下内容,但它可能会像编写的那样工作。有关更多说明,请参见内联注释:

// Only proceed once the DOM has been constructed
document.addEventListener('DOMContentLoaded', function () {

    // Parse the JSON string into a proper object
    let data = JSON.parse('{"Jon":[...],"Paul":[...]}');

    // References to both SELECT elements
    let child = document.getElementById('Child');
    let pet = document.getElementById('Pet');

    // An event listener to handle changes to the child-select
    if (child && pet) {
        child.addEventListener('change', function () {
            // Attempt to look-up the selected value in our JSON
            let option = data[this.value];

            // Proceed only if a match (with depth) is found
            if (option && option.length) {
                // Create a fragment, and add option elements
                let fragment = document.createDocumentFragment();

                option.forEach(function (item) {
                    let entry = document.createElement("option");
                    entry.value = item.name;
                    entry.textContent = item.name;
                    fragment.appendChild(entry);
                });

                // Clear the pet-select of any values, re-populate
                pet.innerHTML = '';
                pet.appendChild(fragment);
            }
        });
    }

});

如果您试图将宠物链接到它们的主人,您可以使用JQuery链接插件

var值={ 乔恩:[{姓名:狗,年龄:5},{姓名:猫,年龄:4},{姓名:鹦鹉,年龄:3}], 保罗:[{名字:鹦鹉,年龄:3}] } $function{ 对于var输入值{ 对于变量i=0;i因此,如果JSON文件中存在Child,您希望相应对象的name属性作为Pet的值列出?这正是我所希望的want@littlewombat您的Java脚本甚至在创建之前就已经运行了。因此,对getElementById的调用无法找到元素。要么将文件放在文件的底部,要么将其全部包装在DOMContentLoaded处理程序中。
<html>
    <head>
    <script src="https://code.jquery.com/jquery-1.9.1.min.js"></script>
    <script>
// Parse the JSON string into a proper object
        let data = JSON.parse('{"Jon":[{"name":"dog","age":5},{"name":"cat","age":4},{"name":"parrot","age":3}],"Paul":[{"name":"parrot","age":3}]}');

        // References to both SELECT elements
        let child = document.getElementById('Child');
        let pet = document.getElementById('Pet');

        // An event listener to handle changes to the child-select
        child.addEventListener('change', function () {
            // Attempt to look-up the selected value in our JSON
            let option = data[this.value];

            // Proceed only if a match (with depth) is found
            if (option && option.length) {
                // Create a fragment, and add option elements
                let fragment = document.createDocumentFragment();

                option.forEach(function (item) {
                    let entry = document.createElement("option");
                    entry.value = item.name;
                    entry.textContent = item.name;
                    fragment.appendChild(entry);
                });

                // Clear the pet-select of any values, re-populate
                pet.innerHTML = '';
                pet.appendChild(fragment);
            }
        });
    </script>
    </head>

    <body>
        <select id="Child">
          <option value="Jon">Jon</option>
          <option value="Paul">Paul</option>
        </select id="Pet">
        <select id="Pet">
        </select>       
    </body>
</html>
// Only proceed once the DOM has been constructed
document.addEventListener('DOMContentLoaded', function () {

    // Parse the JSON string into a proper object
    let data = JSON.parse('{"Jon":[...],"Paul":[...]}');

    // References to both SELECT elements
    let child = document.getElementById('Child');
    let pet = document.getElementById('Pet');

    // An event listener to handle changes to the child-select
    if (child && pet) {
        child.addEventListener('change', function () {
            // Attempt to look-up the selected value in our JSON
            let option = data[this.value];

            // Proceed only if a match (with depth) is found
            if (option && option.length) {
                // Create a fragment, and add option elements
                let fragment = document.createDocumentFragment();

                option.forEach(function (item) {
                    let entry = document.createElement("option");
                    entry.value = item.name;
                    entry.textContent = item.name;
                    fragment.appendChild(entry);
                });

                // Clear the pet-select of any values, re-populate
                pet.innerHTML = '';
                pet.appendChild(fragment);
            }
        });
    }

});