Created
January 6, 2011 21:36
-
-
Save johnsome/768647 to your computer and use it in GitHub Desktop.
use jquery to insert hidden checkbox for unchecked check boxes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
The rails check_box form helper inserts a hidden checkbox to handle unchecked check boxes. This doesn't work when the check box is within an array-like parameter - i.e. project[invoice_attributes][blah]. | |
So - below will append a hidden form field with a value - so it can be handled in your controller or model etc. | |
for: | |
<%= check_box_tag("instance[data][blah]") %> | |
jquery: | |
$("#instance_form").submit(function() { | |
// insert hidden checkbox with value 0 - so unchecked checkboxes submit a value | |
$('input:checkbox').not(':checked').each(function() { | |
$("#instance_form").append("<input type='hidden' name='" + this.name + "' value='0' />"); | |
}); | |
return true; | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks a lot