简体   繁体   中英

Append value to input field using jQuery

I have a small form with three fields and I can submit that form choose different options next time. I am submitting this form with ajax call.

Now on form submit I want to post all the selected options. For this, I have created a hidden field like

<input type="hidden" name="selectedproduct[]" id="sel-product">

and ajax success method am appending value like

 $('#sel-product').val(element.product);

But here I am getting only one value instead of an array. How can I append all the values to the field?

Assuming the product fields with class ".element_product".

//init the array products
arrp=[];


//loop the elements
    $('.element_product').each( function () 
      {
            arrp.push( $(this).val() );
      });

//Pass the array to field
$('#sel-product').val(arrp);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM