mardi 5 janvier 2016

how to remove excess data in javascript array [on hold]

I have some HTML form which looks like this:

<input type='checkbox'  name='lecture'  value='1' >
<input type='checkbox'  name='lecture'  value='2' >
<input type='checkbox'  name='lecture'  value='3' >
<input type='checkbox'  name='lecture'  value='4' >

I also have some jQuery code that adds all checked values to an array and this array goes to the input value:

$('#separate_course_list').on("change", ":checkbox", function() {
  var values = $('input:checkbox:checked').map(function() {
    return this.value;
  }).get(); // ["1", "2", "4"]
  $("input[name='comment']").val(values); //this input saves array
});

In the console.log, I have this (lets pretend that I check first checkbox, then third, then second):

["1", "815643", "753327", "752023"] // checked first checkbox
["1", "3", "815643", "753327", "752023"]//checked third checkbox
["1", "3", "2", "815643", "753327", "752023"]// checked second checkbox

The slice(-1,3) is not working.




Aucun commentaire:

Enregistrer un commentaire