繁体   English   中英

尝试根据下拉菜单中的用户选择从json文件中提取输出

[英]Trying to extract output from a json file based on user selection in a drop-down-menu

我正在尝试为用户提供一个下拉列表,并根据用户的选择提供输出。 下拉列表选项是通过json文件填充的,我希望输出来自json(基于课程的选择)。

这是json:

{
  "101":  {"title":"a title", 
           "shelf":"shelf 1",
           "floor":"1",
           "udcStartAt":"123.3",
           "udcEndAt":"133.29"},
  "102":  {"title":"b title", 
           "shelf":"shelf 2",
           "floor":"1",
           "udcStartAt":"133.3",
           "udcEndAt":"134.69"},
  "103":  {"title":"c title", 
           "shelf":"shelf 3",
           "floor":"1",
           "udcStartAt":"134.7",
           "udcEndAt":"136.59"}
}

这是我当前的javascript:

//this one gets the json and sends it to the jsonToDdl(data) function

function simpleJsonShow(){
  $.getJSON("test.json", jsonToDdl);
}

//this one populate the drop-down-list

function jsonToDdl(data){
  for (sNumber in data){
    $("#selectNumberTwo").append("<option>" + sNumber + "</\option>");
    }
}

我希望输出为每个选项的嵌套值,例如-选择101时,输出应为:1,123.3,133.29。

我在这里寻找解决方案但我不完全了解那里介绍的方法。

我知道了

function showNestedOutput(){
$.getJSON("data\\topicToShelf.json", showNestedResult);  /*get json file and pass to showNestedResult()*/
function showNestedResult(data){
    var selection = $("#selectTopic option:selected").text(); // the var 'selection' stores user's selection text
    var nestedResult = "your result" + data[selection].title + " located: " + data[selection].shelf + " floor: " + data[selection].floor;
    $("#outputTopics").html(nestedResult);
    }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM