How to add an onCha...">

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?

+6
asp.net-mvc
source share
3 answers

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)

+4
source share

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();" }); 
+2
source share

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

0
source share

All Articles