Reputation: 63
I have a jQuery get that receives json five numbers. In the get I have an each function that should be appending to the original string instead it combines all of the data together. I have tried getJSON instead of get with a 'json' var at the end of the function. Any Ideas?
My '/?a=getrigs" JSON
[["50","81","82","85","86"]]
My jQuery:
var rigselect = "<select id='myid'>";
$.get("?a=getrigs", function(rigdata) {
$.each(rigdata, function(rigkey, rigval) {
rigselect += "<option value='" + rigval + "'>" + rigval + "</option>";
});
rigselect += "</select>";
}, "json");
what it generates:
<select id='myid'>
<option value="50,81,82,85,86">50,81,82,85,86</option>
</select>
Upvotes: 1
Views: 65
Reputation: 700910
The JSON becomes an array of arrays when you deserialise it.
When you loop through the items in the outer array there will be only one item, which is an array. When you concatenate the array with strings the array will be converted to a string, which is the items in the array separated by commas.
Change the JSON to just an array:
["50","81","82","85","86"]
or loop through the inner array instead of the outer:
$.each(rigdata[0], function(rigkey, rigval) {
Upvotes: 2