javascript - jQuery each() 的 JSON 结构

标签 javascript jquery json ajax

我似乎不知道如何使用 jQuery 来操作我的 JSON。我有一个 ajax 查询,它使用一些要用作选择选项的键/值对正确回复。

[{"Value":"???","Text":"??? - Unknown"},
{"Value":"AAA","Text":"AAA - A Company"},
{"Value":"BBB","Text":"BBB - B Company"},
{"Value":"CCC","Text":"CCC - C Company"},
{"Value":"DDD","Text":"DDD - D Company"},
{"Value":"EEE","Text":"EEE - E Company"},
{"Value":"FFF","Text":"FFF - F Company"}]

我的jQuery如下

$(function () {

    $("#sqlServerControl").change(function () {
        var sqlServer = $(this).find(":selected").val();
        $.ajax({
            url: "http://localhost/ReportGroupsHelper/Ajax/GetOrganisationData.cshtml",
            type: "GET",
            data: "sqlserver=" + sqlServer,
            dataType: "json",
            success: function (data) {
                var options, index, select, option;

                // Get the raw DOM object for the select box
                select = $("#organisationControl");

                // Clear the old options
                if (select.options != null) {
                    select.options.length = 0;
                }

                // Load the new options
                //options = data.options;
                //for (index = 0; index < options.length; ++index) {
                //    option = options[index];
                //    select.options.add(new Option(option.text, option.value));
                //}

                $.each(data, function (key, value) {
                    select.append($("<option/>", {
                        value: key,
                        text: value
                    }));
                });
            }
        });

    });
});

你可以看到我在哪里尝试了不同的方法,现在已经注释掉了。目前它命中 $.each 然后跳过过去。目标控件的 html 只是

    <fieldset>
        <label for="organisationControl">Organisation</label>
        <select name="organisation" id="organisationControl"></select>
    </fieldset>

最佳答案

value 不是您在 each() 中查找的值。您需要使用 value.Valuevalue.Text

var data = [{"Value":"???","Text":"??? - Unknown"},
{"Value":"AAA","Text":"AAA - A Company"},
{"Value":"BBB","Text":"BBB - B Company"},
{"Value":"CCC","Text":"CCC - C Company"},
{"Value":"DDD","Text":"DDD - D Company"},
{"Value":"EEE","Text":"EEE - E Company"},
{"Value":"FFF","Text":"FFF - F Company"}];

var options, index, select, option;

// Get the raw DOM object for the select box
select = $("#organisationControl");

// Clear the old options
if (select.options != null) {
    select.options.length = 0;
}

// Load the new options
//options = data.options;
//for (index = 0; index < options.length; ++index) {
//    option = options[index];
//    select.options.add(new Option(option.text, option.value));
//}

$.each(data, function (key, value) {
    select.append($("<option/>", {
        value: value.Value,
        text: value.Text
    }));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<fieldset>
    <label for="organisationControl">Organisation</label>
    <select name="organisation" id="organisationControl"></select>
</fieldset>

关于javascript - jQuery each() 的 JSON 结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47563778/

相关文章:

android - 如何在 android 中将游标对象转换为 XML 或 JSON

javascript - 如何确保CSS :hover is applied to dynamically added element

javascript - 水平毛刺线错误

jquery - 你能 !important 整个 css 文件吗?

javascript - 通过前缀抓取ID

ruby - 我如何从 ruby​​ 访问 gmail 收件箱?有什么api吗?

javascript - 嵌套数组中的 jQuery .each()

javascript - WebCenter 站点样式表

javascript - Greasemonkey(使用 waitForKeyElements 实用程序)- 如何在屏幕上显示特定元素后调用函数

PHP - UNSET 深度混合索引/关联数组中的元素