5

Is there anything special I should be doing in ASP.NET when I want to submit a form when a checkbox is clicked. This is some sample HTML I am using...

<form method="post" action="#">
                <input id="hi" class="hidden-field" type="checkbox" value="true" onclick="this.form.submit();" name="hi">hi</input>
            </form>

I tested this in JSFiddle and when you click the checkbox, it naturally posts the form. Somehow I can't get this working inside a MVC PartialView.

David James Ball
  • 903
  • 10
  • 26
  • 1
    If it works in jsFiddle but not in your MVC view, what's different about the two? Does the `input` in your MVC view have a parent `form`? – David Sep 29 '13 at 20:53

1 Answers1

8

Use Javascript/jQuery:

$(document).on("click", "#hi", function(){
    if ($(this).is(':checked')) {
        $('form').submit();
    }
});

All you need is to bind a function on the click event, and in that function, call submit() manually.

ganders
  • 7,285
  • 17
  • 66
  • 114
Blaise
  • 21,314
  • 28
  • 108
  • 169
  • 1
    Thank you. Although this wasn't the exact answer it let me to figure out the solution. I was using the Foundation responsive framework and apparently the checkboxes in their form framework respond to 'onchange' not 'onclick'. – David James Ball Sep 29 '13 at 21:19
  • Glad it is helpful to you. – Blaise Sep 29 '13 at 21:22
  • 1
    You forgot a close bracket on the if. But otherwise, this has helped me, thanks very much! – NickBeaugié Aug 27 '18 at 11:18