user1238353 user1238353 - 7 months ago 118
Javascript Question

How to get multiple checkbox value and assign to hidden field

I want to store the multiple id to hidden field.
So value able to bind to controller.

<form:hidden id="ids" path="ids" value="${ids }"/>


When click button delete will call jquery to delete row.

var deleteIds = [];
$("#deleteRow").on('click', function() {
deleteIds = $('.case:checkbox:checked').val();
$('.case:checkbox:checked').parents("tr").remove();
$('#ids').val(deleteIds);
});


My question is


  • How to set the value into ids?



Thank You.

Answer

I able to set the multiples value to hidden field. Answer as below.

 <form:hidden id="ids" path="ids" value="${ids }"/>



$("#deleteRow").on('click', function() {
                var deleteIds = [];
                $('.case:checkbox:checked').each(function(i){
                    deleteIds[i] = $(this).val();
                });
                $('#ids').attr("value",deleteIds);
  });

The each(function(i)) will loop all the checkbox and store in array[], after that assign the array to hidden field.