Neutralise
Neutralise

Reputation: 507

jQuery submit form on checkbox press

I am trying to submit a form on checking or unchecking a checkbox using jQuery and using the data value of that checkbox, can someone help me on how to do this?

Upvotes: 5

Views: 5196

Answers (2)

Jacob Lowe
Jacob Lowe

Reputation: 688

i think you should try using .trigger

i think there is a trigger submit

$('#yourElement').live('click', function(){
    $('#yourForm').trigger('submit');
})

Upvotes: 1

Ken Redler
Ken Redler

Reputation: 23943

The requirement of submitting the value of the box you just un-checked is the unusual bit here. One option is to stuff that value into a hidden field, which will be submitted. So:

<input name="checkbox_trigger" id="checkbox_trigger" value="" type="hidden" />

Then:

$('#yourcheckbox').click( function(e){
  $('#checkbox_trigger').val( e.value ); // capture on check or uncheck
  $(this).closest('form').submit();
});

If the name of the checkbox is also significant, you can easily follow this pattern to capture the field name as well.

Upvotes: 2

Related Questions