Asp.net mvc: mimics auto-repeat for simple checkbox
I have a simple checkbox generated with:
<%= Html.CheckBox("myCB" )%> How to add an onChange handler to this that sends?
Add an onClick handler to the CheckBox, which represents the form that the CheckBox belongs to ... quick, clickHandler without a code example:
<%= Html.CheckBox("myCB", new { onClick = "$(this).parent('form:first').submit();" }); (example definitely not tested for accuracy)
If you have only one form and you are not using jQuery (you should be, by the way), try the following:
<%= Html.CheckBox("myCB", new { onClick = "document.form.submit();" }); I would highly recommend using jQuery to support this, because it makes it easy to add behavior to a checkbox on your site using a selector of either an identifier or a class. You can then put the script anywhere on the page or in an external .js file.
<script language="javascript" type="text/javascript"> $('#myCB').click(function() { $(this).parent('form:first').submit(); }); </script> Alternatively, the selector may be based on a class (or any attribute, for that matter). More details here: http://docs.jquery.com/Selectors