mardi 10 mai 2016

Javascript Form Using "$(this)"

I have an html form with a bunch of checkboxes. Each checkbox is described as this:

echo "<td>"
    echo "<input type = \"checkbox\" name=\"name\" value=\"$name\">";
    echo "<input type = \"hidden\" name=\"user\" value=\"$user\">"; 
echo "</td>"

Then I have a submit button that when clicked will run some javascript:

echo "<input type=\"button\" name =\"send\" value=\"Submit\" onclick=\"get_checked(form)\">";

Now my javascript:

function get_checked(form){
    $("input[type='checkbox']").change(function() {         
        if( $(this).is(':checked') ){
            $(this).find("input[name]").each(function(index, node){
                formData[node.name] = node.value;
                alert (node.name + node.value);
            });
        }
    });
}

I want to find every checked checkbox and then print out each of the name/value pairs for the checkbox. But it is not working as nothing gets alerted and I think I might be using $(this) improperly because this works when I use .each:

$(':checkbox:checked').each(function(i){
    testname_checked[i] = $(this).val();
    alert(testname_checked[i]);
});

Any ideas?

Aucun commentaire:

Enregistrer un commentaire