jquery - MVC Controller returning JSON to populate Select list but value needs to have a colon -
we have mvc controller returns json want.
populate select list options values have colon in them. json: {result: {["valueone": "option 1: option", "valuetwo" : "option 2: other option"]}
we populating list jquery $.ajax
$("#eventnamedropdownlist").change(function () { geteventdatesandtimes(); }) function geteventdatesandtimes() { var eventname = $('#eventnamedropdownlist').val();// on eventnamedropdownlist value coming "option 1:" instead of whole value. var url = '@url.action("geteventdatetimebyname", "product")'; $("#eventidanddatedropdownlist").find('option').remove().end(); // clear before appending new list $.ajax({ url: url, type: "get", cache: false, datatype: "json", data: { eventname: eventname }, success: function (data) { console.log("success"); if (data.eventdatetimelist.length) { $.each(data.eventdatetimelist, function (i, event) { console.log("foreach: " + event.eventname); $("#eventidanddatedropdownlist").append("<option value=" + event.startdatetime + ">" + event.startdatetime + "</option>") }); } else { $("#eventidanddatedropdownlist").find('option').remove().end(); // clear before appending new list console.log("result no data"); } }, error: function () { $("#eventidanddatedropdownlist").find('option').remove().end(); // clear list alert("an error occurred getting event names"); } }); }
which working populate option list fine.
example want happen:
<select> <option value="option 1: option">option 1: option</option> <option value="option 2: option">option 2: other option</option> </select>
but comes out
<select> <option value="option 1:" option>option 1: option</option> <option value="option 2:" option>option 2: other option</option> </select>
as can see in option value property string terminates after colon, whole value json string colon not surrounded quotes.
simply concatenate using '
like
'<option value="' + event.startdatetime + '">' + event.startdatetime + '</option>'
Comments
Post a Comment