【问题标题】:populate dropdown with particular value from json array (jQuery)使用 json 数组(jQuery)中的特定值填充下拉列表
【发布时间】:2015-04-22 12:47:56
【问题描述】:

我正在尝试使用来自多个 json 对象的 'startDate' 的值填充下拉列表,如何在不显示 'endDate' 和 'price' 的情况下做到这一点?

这是我目前的情况:

这是我正在使用的代码:

function get_months(year) {
    selected_year = year;
    date.style.visibility = 'hidden';
    month.style.visibility = 'visible';

    yearArray = dateType[year];
    console.log(yearArray);

    $.each(yearArray, function(key, value) {
        $(month).append('<option value=' + value + '>' + key + '</option>');
    });
}

function get_dates(month) {
    date_selected = false;

    if(current_type == 'Weekend') {
        date.style.visibility = 'visible';
        //console.log(current_customer, current_type, selected_year, month);
    } else if(current_type == 'Midweek') {
        date.style.visibility = 'visible';
        //console.log(current_customer, current_type, selected_year, month);
    } else if(current_type == 'Week') {
        date.style.visibility = 'visible';
        //console.log(current_customer, current_type, selected_year, month);
    }
    monthArray = yearArray[month];
    console.log(monthArray);
    alert(monthArray);

    $.each(monthArray, function(key, value) {
        //startDate = value.startDate;
        console.log(value.startDate);
        $.each(value, function(key2, value2) {
            /* console.log(key2);
            console.log(value2); */
            $(date).append('<option value=' + key2 + '>' + value2 + '</option>');
        });
    });
}

如果需要任何其他信息,请询问! 提前致谢!

【问题讨论】:

  • 您到底在寻找什么?您想从 JSON 中提取日期部分吗?
  • @MokshShah 正如您在当前情况的图像中看到的那样,它显示了所有 3 个对象的“startDate”、“endDate”和“price”。我只想在所有 3 个对象的下拉列表中显示“startDate”的值。
  • 你能发布你的 JSON 吗?

标签: jquery arrays json


【解决方案1】:

thread 回答了类似的问题,它具有三种不同的方法/解决方案。

  1. 读取 json、迭代和打印值。
  2. 通过 css 隐藏/显示
  3. 附加 json kvs。

希望这会有所帮助。

【讨论】:

    【解决方案2】:

    你可能需要稍微调整一下,因为我没有正确分析你在做什么,但看起来你需要做一些改变:

            $(date).append('<option value=' + key2 + '>' + value2 + '</option>');
    

    到:

            if (key2==='startDate') {
                $(date).append('<option value=' + key2 + '>' + value2 + '</option>');
            }
    

    【讨论】:

    • 我猜,key2 本身就是一个值,这个key2==='startDate' 将如何工作?
    • 不敢相信我没有想到一个简单的 if 语句。谢谢!我会尽快接受。
    猜你喜欢
    • 1970-01-01
    • 2013-04-16
    • 1970-01-01
    • 1970-01-01
    • 2011-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多