javascript - Jquery get all checked box and group by checkbox name -
hi have checboxes different names:
names - hours, , value - day. example: value="1" means monday, , name="0609" means 06am - 09am.
<input type="checkbox" value="1" name="0609"> <input type="checkbox" value="2" name="0609"> ... <input type="checkbox" value="7" name="0609"> <input type="checkbox" value="1" name="0912"> <input type="checkbox" value="2" name="0912"> ... <input type="checkbox" value="7" name="0912">
i need take checked , non checked inputs , combine array name example: if days checked except monday
'0609' => 0, 1, 1, 1, 1, 1, 1
how can solve ?
i trying .map ungrouped values...
var valuess = $('input:checked').map(function() { return this.value; }).get(); console.log(valuess);
update:
var obj = {}; // define object // loop on checkboxes $('input[type=checkbox]').each(function() { var name = $(this).attr('name'); // name of checkbox var sthisval = (this.checked ? "1" : "0"); if (obj[name]) { obj[name].push(sthisval); // push value } else { obj[name] = [sthisval]; // create array , push value } }); console.log(obj);
check this:
var obj = {}; // define object // loop on checkboxes $(':checkbox').each(function() { var name = $(this).attr('name'); // name of checkbox if (obj[name]) { obj[name].push($(this).val()); // push value } else { obj[name] = [$(this).val()]; // create array , push value } }); console.log(obj);
Comments
Post a Comment