Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC submitting form on checkbox click

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.

like image 308
David James Ball Avatar asked Sep 29 '13 20:09

David James Ball


1 Answers

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.

like image 183
Blaise Avatar answered Nov 07 '22 03:11

Blaise