aneuryzm
aneuryzm

Reputation: 64834

jQuery: submit a form when the checkbox is selected

I have a form, and I want automatically to update it when I click on a checkbox. This is my current code:

$('.option img').click(function() {
     $("#views-exposed-form-Portfolio-page-1").submit();
});

However, the selected checkbox is not stored before the form is submitted, and so the selection has not effect.

thanks

Upvotes: 1

Views: 2207

Answers (4)

Tgr
Tgr

Reputation: 28170

You could use the change event instead of the click event, or just select it manually.

Upvotes: 0

Tim
Tim

Reputation: 7056

if ($(".checkbox-class:checked")) {

$("#views-exposed-form-Portfolio-page-1").submit();   

}

Upvotes: 0

brettkelly
brettkelly

Reputation: 28205

$("#myCheckBox").change(
    function(){
        $("#views-exposed-form-Portfolio-page-1").submit();   
    }
);

You can obviously check and see if the checkbox is checked before submitting the form, but this way is easier than submitting based on the click event (which fires before the change event, it seems).

Upvotes: 5

Toby
Toby

Reputation: 8792

If you are using that as the final thing then surely you can just store the reverse of what was already stored for it?

If it is always set to off, then you can always store 1 or if it could already have been stored then find that value and return the inverse of it.

Upvotes: 0

Related Questions