Sush
Sush

Reputation: 1457

displaying values from json in the dropdown list

http://jsfiddle.net/VY7pJ/6/

Please have a look this.

Here while i selecting DUI Config values(DGI,2ddDGI) i have to display the correpsonding description values in place of 'display DUI description'. How it is possible..Code am using is also mentioned in the jfiddle

$.each(data.VMs, function (i, vm) {
     if (vm.ID === selected) {
         $.each(vm.ADAPTER, function (j, ad) {
             adapter.push('<option value="' + vm.ADAPTER[j].names + '">' + vm.ADAPTER[j].names + '</option>');
             dui.push('<option value="' + vm.DUIConfig[j].ID + '">' + vm.DUIConfig[j].names + '</option>');
             $('#label').html(vm.DUIConfig[0].description);
             $("#dui").change(function () {

             });
         });
     }
 });

Upvotes: 0

Views: 70

Answers (1)

Dhaval Marthak
Dhaval Marthak

Reputation: 17366

Problem is because of following

"ID": "VM-WIN7-64"

And you have initialized

selected = 'VM-WIN764';

You're checking if(ID===selected) which is always false! So your if condition is never satisfied.

Change selected = VM-WIN7-64 instead 'VM-WIN764' and everything works!

Fiddle Demo


Update

You can fire change() of your dui dropdown and can get the description values. Try with the following code:

if (vm.ID === selected) {
        $.each(vm.ADAPTER, function (j, ad) {
            adapter.push('<option value="' + vm.ADAPTER[j].names + '">' + vm.ADAPTER[j].names + '</option>');
            dui.push('<option value="' + vm.DUIConfig[j].ID + '">' + vm.DUIConfig[j].names + '</option>');
            $('#label').html(vm.DUIConfig[0].description);

        });
        //Written Change event here
        $("#dui").change(function () {
            $('#label').html(vm.DUIConfig[this.selectedIndex].description); //Get selected value
        });
    }

Final Fiddle

Upvotes: 3

Related Questions