Is there a way to publish all the values ​​in a list of multi-selections, not just selected ones?

I have two MultiSelect lists (AllProductList and SelectedProductList), AllProductList contains all products for a specific category, and I add / clone options from AllProductList to SelectedProductList using JQuery.

I obviously only want to post the values ​​to the SelectedProductList and regardless of whether they are selected or not.

I wrapped the form tags only in the SelectedProductList, and now you need to somehow publish all the parameter values ​​in it, regardless of whether they are selected or not.

+6
source share
2 answers

you can write javascript that fills the hidden form element with all the values ​​from the selection, something like below, and on the server just use explode (",", $ _ POST ["allValues"]) to get all the parameters

<script> var hiddenValues = ""; $(document).ready(function(){ $("#mySelect option").each(function(){ hiddenValues = $(this).val() + ","; })//end each $("#myForm").append("<input type='hidden' name='allValues' value='"+hiddenValues+"'>") }) </script> 

obviously the above has a jQuery dependency and your form has the identifier myForm and that your multiselect has the id mySelect :)

EDIT:
NOTE 1: this saves only parameter values, not labels from select (a similar method can be used to save these parameters). just keep that in mind

NOTE2: be careful if the values ​​contain any commas, as this will invalidate your input (if not escaped in any way or if no other delimiter is used)

+2
source

I can not comment, so probably I should answer ...

Bogdan answers + =

 hiddenValues += $(this).val() + ","; 

otherwise you only get 1 ...

+1
source

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


All Articles