Reputation: 156
I have an alpaca form with select ui element where I can select multiple values and save the form.When i save the form the JSON using JSON.stringify(val)
the generated of the form is having problem. JSON is having only ids of selected elements.I want the JSON containing both the ids and values
<select id="alpaca4" multiple="multiple" size="5" name="roles" class="alpaca-control form-control">
<option value="1">one</option>
<option value="2">two</option>
</select>
The generated json format is :
{"roles":["1","2"]}
The format iam expecting is :
{"roles":["1":"One","2":"Two"]}
Upvotes: 1
Views: 1270
Reputation: 81
You should only persist the values, not the labels. Alpaca is doing the right thing here according to how html forms work.
Upvotes: 0
Reputation: 36609
Use
jQuery#map
var mapped = $('#alpaca4 option').map(function() {
var obj = {};
obj[this.value] = this.textContent;
return obj;
});
console.log(mapped.get());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<select id="alpaca4" multiple="multiple" size="5" name="roles" class="alpaca-control form-control">
<option value="1">one</option>
<option value="2">two</option>
</select>
Upvotes: 3