You must accept the values ​​from the selected checkboxes to the input location of the input file, separated by commas

I am trying to make a series of selected checkboxes and create a comma separated list in the input box. This slightly exceeds my jQuery skills.

Here is an example HTML:

<input type="checkbox" value="Bill" id="CAT_Custom_173676_0" name="CAT_Custom_173676" />Bill<br />
<input type="checkbox" value="Dan" id="CAT_Custom_173676_1" name="CAT_Custom_173676" />Dan<br />
<input type="checkbox" value="Francis" id="CAT_Custom_173676_2" name="CAT_Custom_173676" />Francis<br />

I need to take the values ​​from these checkboxes and paste them into this field

<input type="text" class="cat_textbox" id="CAT_Custom_172786" name="CAT_Custom_172786" maxlength="1024"  />

So, if all the checkboxes above are checked, the value of the input field will be "Bill, Dan, Francis".

This event may occur when a form is submitted.

Any help would be appreciated.

+3
source share
2 answers

map() , jQuery, , get() , join ( "," ) nb :

var arr = ​$("input:checked").map(function () { return this.value; }​);
$("#CAT_Custom_172786").val(arr.get().join(","));

nb: join(), , , . .

+6

, "CAT_Custom_173676"

alert($("input[name=CAT_Custom_173676]:checked").map(function () {
return this.value;}).get().join(","));
+1

Source: https://habr.com/ru/post/1760538/


All Articles