0

I want to submit a form whenever checkboxes are clicked (I already have ajax set up with Rails) - this means even if it gets unchecked. Also I never want the click handler to unbind, so a user can click a checkbox as many times and the form will get submitted each time.

$(":checkbox").click(function() {
  $(this).closest("form").submit();
});

The above code works fine, but it stops working after the first click. How would I make it so that the above code will work even when a user clicks a checkbox more than once?

EDIT:

I am replacing the contents of the form (including the checkboxes) on submission of the form.

6
  • 2
    what exactly happens in the 2nd click? I believe this might be a popup blocking issue... Commented Jul 17, 2011 at 23:54
  • it should work on the second click also, are there any errors or other kind of notifications ? Commented Jul 18, 2011 at 0:03
  • Nothing happens on the second click at all Commented Jul 18, 2011 at 0:17
  • 1
    What’s the effect of the AJAX request? Does anything on the page get updated or replaced? Commented Jul 18, 2011 at 0:28
  • @Sidnicious I'm replacing the contents of the form (including the checkboxes) Commented Jul 18, 2011 at 6:19

1 Answer 1

4
$(':checkbox').live('click',function() {
    $(this).closest('form').submit();
});
Sign up to request clarification or add additional context in comments.

4 Comments

why do you think this will work? I don't see a problem that live can solve. Also, you missed a quote symbol
Additionally, you need to supply an event name to live for this to by syntactically correct.
Ugh long day - code fixed. Depending on how his form is being submitted, I've had issues in the past with event bindings being lost, and applying a future-proof event observer like live or delegate solved the issue.
@AlienWebguy: Looks like this answer is actually correct, judging by the OP's most recent comment, +1.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.