Skip to content
Advertisement

How to append from json result

I have a Db class that called trip, which should have more then one participant, I Want to have a button that each time clicked it will generate a new select element that have an <option> list of all his friend. He should be able to choose more then one friend but to the same friend twice.

I have a JsonResult function in my controller (MVC). I have button that when clicked it will add new select with option for each friend. I tried doing this:

JavaScript

Before that function I have those lines of code:

JavaScript

But it won’t work. Anyone have any idea what will work? Thanks!

Advertisement

Answer

You can not use $.each like that. Have a look at the doc: jQuery.each() API Doc Let’s assume your getJSON call actually returns the right data. And you get an array like this

JavaScript

I would strongly recommend writing some function to append your select and add the options, like so:

JavaScript

and

JavaScript

that way you can use these funcions in your getJSON Method like so:

JavaScript

Working example:

JavaScript
JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement