簡體   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