Yoweli Kachala
Yoweli Kachala

Reputation: 423

checkbox in a json appended table not working

My problem is, I have created a json table and the information is from a mysql database. That's not the problem anyway. i can display the information on the appended table, the checkboxes are there but!!!! only the first checkbox in the first row is working. here is my code:

$.ajax({
    type: 'GET',
    url: 'scripts/actions/get.php',
    dataType: 'json',
    success: function(data) {
        $.each(data,function(i,user){
        $('tr:odd').addClass('odd');
            var tblRow =
                "<tr>"
                +"<td><input name='chk' type='checkbox' id='chk' myid = "+user.id+"></td>"
                +"<td>"+user.id+"</td>"
                +"<td>"+user.name+"</td>"
                +"<td>"+user.principal+"</td>"
                +"<td>"+user.admin_contact_person+"</td>"
                +"<td>"+user.telephone_number+"</td>"
                +"<td>"+user.fax_number+"</td>"
                +"<td>"+user.contact_person_email+"</td>"
                +"</tr>";

                $(tblRow).appendTo("#tbody");

                });
            }
        });

Can some shade some light on this please... :)

Upvotes: 0

Views: 341

Answers (2)

Barmar
Barmar

Reputation: 781726

Change to:

            +"<td><input name='chk[]' type='checkbox' class="chk" value='"+user.id+"'></td>"

You can then use

$(".chk:checked")

to get a collection of all the checkboxes that have been selected. You can iterate through them with .each() to get their values, to know which users have been checked.

Upvotes: 0

Catalin Sterian
Catalin Sterian

Reputation: 96

+"<td><input name='chk' type='checkbox' id='chk' id = "+user.id+"></td>"

issues: you use id 2 times and if you need this to work in a form i suggest:

+"<td><input name='"+user.id+"' type='checkbox' id='chk'/></td>"

because you need different name for each checkbox...

Upvotes: 1

Related Questions