Convert select list to JSON using jQuery -
how can .val()'s , .html()'s of options multiple select list json object? preferably using jquery.
thanks in advance!
a little more info:
i using 2 multiple select boxes. selecting items on left box , moving them right. once have items want selected push submit , take items in right select box , move them main list.
here code using once json object:
datalistobject = json.parse(response); if (datalistobject.length){ $(".data-list tbody").empty(); (var i=0; < datalistobject.length; i++) { var newrow = "<tr><td><input type='checkbox' name='user_id' value='" + datalistobject[i][0] + "' class='data-list-check'></td><td>" + datalistobject[i][1] + "</td></tr>"; $(newrow).appendto($(".data-list tbody")); } }
example html , output be:
<select name="selecteditems"> <option value="op1">option 1</option> <option value="op2">option 2</option> <option value="op3">option 3</option> </select> [ ["op1","option 1"], ["op2","option 2"], ["op3","option 3"] ]
something this?
var items = $("#select > option").map(function() { var opt = {}; opt[$(this).val()] = $(this).text(); return opt; }).get(); for(var = 0; < items.length; i++) { for(key in items[i]) { alert(key + ': ' + items[i][key]); } }
demo: http://jsfiddle.net/engul/
edit: structure example data:
var items = $("#select > option").map(function() { var arr = []; arr.push([$(this).val(), $(this).text()]); return arr; }).get(); for(var = 0; < items.length; i++) { alert(items[i]); }
demo: http://jsfiddle.net/karim79/engul/2/
see .map
.
Comments
Post a Comment