views:

53

answers:

3

Hello all,

I'm having a bit of a problem with this code.

The program gives me a list of checkboxes but a user ID. then u user can change his selection and push the save button (id="btnSaveUserIntersts") and i am trying to save in the hidden textbox all the values of the checkboxes that was choosen.

The problem is that i am getting all the time the same selections that came form the database and not getting the new selection that the user made.

Can any one tell me what am i doing wrong here?

$(document).ready(
function()
{ 
    $('#btnSaveUserIntersts').bind(
        'click',
        function()
        {
            var strCheckBoxChecked = new String();
            $('input[type=checkbox][checked]').each(
                function()
                {
                    strCheckBoxChecked += $(this).val();
                    strCheckBoxChecked += ',';
                }
            );
            $('#hidUserInterests').val(strCheckBoxChecked);
        }
    );
}

);

10x

+3  A: 

Try using:

    $('input:checkbox:checked').each(
        function()
        {
            strCheckBoxChecked += $(this).val();
            strCheckBoxChecked += ',';
        }
    );

As the selector instead of what you're currently using.

Matt
10x, stupid me...lol :-)
Erez
+1  A: 
$('input[type="checkbox"]:checked')
czarchaic
+1  A: 

Use .map, it's far prettier:

var strCheckBoxChecked = $('input:checkbox:checked').map(function()
    return this.value;
}).get().join(",");

And the selector you are using is close, $('input[type=checkbox][checked=checked]')

karim79