我有一个包含三个字段的小表单,我可以在下次提交该表单时选择不同的选项。我正在使用 ajax 调用提交此表单。
现在在表单提交上我想发布所有选定的选项。为此,我创建了一个隐藏字段,例如
<input type="hidden" name="selectedproduct[]" id="sel-product">
和ajax成功方法正在 append 值,例如
$('#sel-product').val(element.product);
但是这里我只得到一个值而不是一个数组。如何将所有值 append 到该字段?
最佳答案
假设产品字段具有类“.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);
关于javascript - 使用 jQuery 将值 append 到输入字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51132543/