mercredi 2 septembre 2015

Change event not firing for dynamicaly added checkbox [duplicate]

This question already has an answer here:

I am dynamically adding rows to a table, and each row has a checkbox. I am trying to associate a checkbox-change event with the checkbox, but it never fires. The relevant code is given below:

$.post("items.php", args, function(response){

    if (response.status == 0){
        console.log(response.data);

        for (var i=0; i < response.data.length; i++){
            var rowContent = "<tr>";
            rowContent += "<td>" + response.data[i]["name"] + "</td>";
            rowContent += "<td>" + response.data[i]["description"] + "</td>";
            rowContent += "<td>" + response.data[i]["height"] + "</td>";
            rowContent += "<td>" + response.data[i]["weight"] + "</td>";
            rowContent += "<td><div class='checkbox'><input type='checkbox' value=''></div></td>";
            rowContent += "<td><div class='form-group'><select class='form-control' disabled>" + addOptions(response.data[i]["quantity"]) + "</select></div></td>";
            rowContent += "</tr>"; 
            $('#item-row').append(rowContent);
        }
    }else{}

})

...

$("input[type='checkbox']").change(function(){

    alert("It works!");
});

Is there something wrong with the location of the change event? It's kind of hard to post a fiddle as there is a lot of code besides this. Glad for any help!




Aucun commentaire:

Enregistrer un commentaire