mardi 22 décembre 2020

How does one, from a checkbox group and preferably with jQuery, collect array data and does assign this data to a hidden target element's value?

I have a group of equally named checkboxes ...

<input type="checkbox" class="checkbox" name="checkbox[]" value="1" checked />
<input type="checkbox" class="checkbox" name="checkbox[]" value="2" checked />
<input type="checkbox" class="checkbox" name="checkbox[]" value="3" checked />

and a hidden form element ...

<input type="hidden" class="target" name="target[]" value="" />

Using jQuery, I want to copy all checked checkbox values in form of a value-list in its stringified array literal notation to the hidden target element's value.

I have tried the following ...

$(".checkbox").each(function(idx, val) {

  $('input[name="target['+idx+']"]').val(this.value);
});

... without success. How can I generate and assign the correct result?




Aucun commentaire:

Enregistrer un commentaire