buck1112
buck1112

Reputation: 502

jquery click vs button executed function to find length of checkboxes

I am currently working with a JQuery function that determines if there are any checkboxes checked in my form. I'm using JQuery v1.10.2

This code works fine whenever I execute it with a button, but when I try to use a checkbox within the form to execute it, it does nothing. I've tested with a JFiddle, and that worked, but in my form, the alert does not fire. I've checked for redundant names, and my form id is unique. Could it be the div/table structures within the form causing some sort of conflict? The form is wrapped inside of a hidden div. Thanks for any help.

Below is the code:

<script>
$(document).ready(function(){
    //$(".ksa_button_check").click(function(){
    $(".ksa_check_k, .ksa_check_s").click(function(){

        if ($("#ksaChecks input:checkbox:checked").length > 0) {
            alert('is checked');
        }

    });
});
</script>

Upvotes: 0

Views: 137

Answers (1)

Bowenac
Bowenac

Reputation: 503

You could use something like this.

var checkboxes = $("input[type='checkbox']");
checkboxes.click(function() {
    alert("Checkbox Is Checked");
});

http://jsfiddle.net/bowenac/aTDj6/1/

Not sure which language you are using. If you are using PHP you could check if $_POST has a value etc. If a checkbox is checked it would have $_POST data if not it would not.

Something like this.

if(isset($_POST['submit'])) {

$checkboxes = isset($_POST['checkbox']) ? $_POST['checkbox'] : array();
foreach($checkboxes as $value) {
Do something
);
};
};

Other than that I am wondering if your checkboxes are losing its state on your form submit. So yea as others said would help to see the code.

Upvotes: 1

Related Questions