jquery - 根据 JSON/Ajax 格式请求选择 2 optgroup 组

标签 jquery json ajax jquery-select2

使用 Select2 和 JSON/Ajax 调用,我正在尝试创建这样的组:

<optgroup label="Property Reference">
     <option value="1">5742</option> 
     <option value="2">5788</option>
     <option value="3">5762</option>
     <option value="4">5711</option>
</optgroup>

我的问题的文档 is not available

<小时/>

我设法开始工作的格式:

{
    results: [
        {
            id: 'CA',
            text: 'California'
        },
        {
            id: 'CO',
            text: 'Colarado'
        ]
    }
}

但是这里没有群组。

已关注 this question ,我尝试了以下格式:

{
    "results": {
        "text": "Mountain Time Zone",
        "children": [
            {
                "id": "CA",
                "text": "California"
            },
            {
                "id": "CO",
                "text": "Colorado"
            }
        ]
    }
}

{
    "results": {
        "Mountain Time Zone": [
            {
                "id": "CA",
                "text": "California"
            },
            {
                "id": "CO",
                "text": "Colorado"
            }
        ]
    }
}

{
    "Mountain Time Zone": [
        {
            "id": "CA",
            "text": "California"
        },
        {
            "id": "CO",
            "text": "Colorado"
        }
    ]
}

但没有一个起作用。有人知道正确的格式是什么吗?

您可以在这里测试:https://jsfiddle.net/5am4zda6/2/

编辑已解决:该死...忘记了[]。 正确的格式是:

{
    "results": [
        {
            "text": "Mountain Time Zone",
            "children": [
                {
                    "id": "CA",
                    "text": "California"
                },
                {
                    "id": "CO",
                    "text": "Colarado"
                }
            ]
        }
    ]
}

最佳答案

尝试这样:

{
    "results": [
        {
            "text": "Groupe 1",
            "children": [
                {
                    "id": "CA",
                    "text": "California"
                },
                {
                    "id": "CO",
                    "text": "Colarado"
                }
            ]
        },
        {
            "text": "Groupe 2",
            "children": [
                {
                    "id": "CA",
                    "text": "California"
                },
                {
                    "id": "CO",
                    "text": "Colarado"
                }
            ]
        }
    ]
}

我想你已经忘记了[就在“结果”之后

关于jquery - 根据 JSON/Ajax 格式请求选择 2 optgroup 组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38763514/

相关文章:

javascript - 通过 xmlhttprequest 上传文件生成多部分 : NextPart: EOF

javascript - 将 JavaScript JQuery 添加到 HTA

javascript - 如何为特定的输入按钮指定action属性?

c# - C# 中的 JSON 到 ListView

json - 更新逻辑应用中的 json

jquery - Rails 4 如何捕捉 ajax :success event

python - WSGI:使用 AJAX 从 python 脚本中获取字符串

javascript - ajax 未在声明的变量上设置值

javascript - 定位 Lightbox2 关闭按钮时遇到问题

html - 通过属性进行 polymer 数据绑定(bind)