Umieść wartości pola wyboru w ukrytym wejściu za pomocą jQuery

Chciałbym wypełnić ukryte dane wejściowe wartościami wybranych pól wyboru z odstępem między tymi wartościami. Próbowałem, co teoretycznie powinno działać, ale to nie jest…

JS:

$(document).ready(function () {


      var vals = $(':checkbox:checked').map(function(){
         return $(this).val();
      }).get().join(',');

      // save the values to a hidden field
      $('#tags').val(vals);


});

HTML

 <form>

  <input type="checkbox" value="test1" id="test1"><label>Test</label>
  <input type="checkbox" value="test2" id="test2"><label>Test2</label>
  <input type="checkbox" value="test3" id="test3"><label>Test3</label>

  <input type="text" value="" id="tags">

</form>

Jakieś pomysły?

questionAnswers(2)

yourAnswerToTheQuestion