简体   繁体   中英

JSON undefined data in a dependent dropdown

I have three dropdowns called Platform, Tasktype and Component which is dependent on each other. I have figured out until the second dropdown to get relevant data however the third dropdown gets undefined.

See working demo below.

http://plnkr.co/tFZhcTm1XeqK3NoHdG4z

JQuery

$(function() {
var platforms;
var tasktypes;
var compos;
var jsonData;


$.getJSON('tasks.json', function(result) {
jsonData = result;

$.each(result, function(i, platform) {
platforms += "<option value='" +
  platform.name +
  "'>" +
  platform.name +
  "</option>";
});
$('#platform').html(platforms);
}); 

$("#platform").change(function (){
var idx = $("#platform").prop('selectedIndex');
var platforms = jsonData[idx].task;

tasktypes = "";
for (i = 0; i < platforms.length; i++) {
  tasktypes += "<option value='" +
    platforms[i].taskname +
    "'>" +
    platforms[i].taskname +
    "</option>";
};
$('#taskname').html(tasktypes);
});


$("#taskname").change(function (){
var idc = $("#taskname").prop('selectedIndex');
var tasktypes = jsonData[idc].task;

compos = "";
for (i = 0; i < tasktypes.length; i++) {
  compos += "<option value='" +
    tasktypes[i].componentname +
    "'>" +
    tasktypes[i].componentname +
    "</option>";
};
$('#components').html(compos);
});  
});

I want to get relevant component names once platform and task type is selected.

   $("#taskname").change(function (){
    var idc = $("#taskname").prop('selectedIndex');
    var tasktypes = jsonData[idc].task;

console.log(tasktypes); // include and see what is coming over here in console 

    compos = "";
    for (i = 0; i < tasktypes.length; i++) {

console.log(tasktypes[i]); // include and see what is coming over here in console  

      compos += "<option value='" +
        tasktypes[i].componentname +
        "'>" +
        tasktypes[i].componentname +
        "</option>";
    };
    $('#components').html(compos);
    });  
    }); 

some data is missing somewhere or script doesnt understand what is coming that what happens i believe

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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