javascript - Jquery - Grab multiple selections' data in Jquery -
this originates my original question. i'm expanding on it.
html select options
<select id="1d" name="camp" multiple="multiple"> <option data-url0="week_1" value="week 1">30th july</option> <option data-url1="week_2" value="week 2">6th august</option> </select> <input type="hidden" name="camp_url0" id="1e"> <input type="hidden" name="camp_url1" id="1f"> jquery script i'm struggling with.
$("#1d").on("change", function () { var url1 = $(this).children(":selected").data("url0"); var url2 = $(this).children(":selected").data("url1"); $("#1e").val(url0); $("#1f").val(url1); }); this code works beautifully (maybe not cleanest?), except 1 important issue. though multiple selector, whenever both options selected, 1 option marked :selected in dom, meaning 1 data-url{row_id} being inputted. need both, if both selected.
i hope makes sense. help.
upd:
add additional “routing” data html
<select id="1d" name="camp" size="5" multiple> <option data-url="week_1" data-id="1e" value="week 1">30th july</option> <option data-url="week_2" data-id="1f" value="week 2">6th august</option> </select> and use it
$("#1d").on("change", function () { $('input[type=hidden]').val(''); $('option:selected', this).each(function() { $('#' + $(this).data('id')).val($(this).data('url')) }) }) old:
just .map , array data.
$('option:selected', this).map(function() { return $(this).data('url') }) ["week_1", "week_2"]
Comments
Post a Comment