Skip to content Skip to sidebar Skip to footer

Get The Values From The Multiple Checkbox And Append It To Html?

I'm using the modal from twitter bootstrap, i want to make a modal that when a user click on button it appear with checkboxes that have a specific values, and when modal hide the v

Solution 1:

I think the problem is when you do allVals.push(checkbox_val); inside the .each loop, you are pushing the same value all the times, that's why you got [1,1,1]. So update your jQuery:

jQuery(document).ready(function($) {
    $('#myModal .btn').last().on('click', function(){
        varMyModal = $('#myModal');
        functionget_value() {
            var allVals = [];
            $('input:checkbox:checked').each(function() {
                allVals.push($(this).val()); //Use the $('this') selector
            });

            $('a.btn').after("<p>" + allVals + "</p>");
        }
        MyModal.modal('hide').on('hidden', function(){
           get_value();

        })

    });
  });

Solution 2:

In console the array returns "1","1"

$('input:checkbox:checked').each(function() {
    allVals.push(checkbox_val);// it will return first checkbox value '1' alway
});

For this replace this line allVals.push(checkbox_val); to allVals.push($(this).val());

Post a Comment for "Get The Values From The Multiple Checkbox And Append It To Html?"