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.

1
  • 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? Commented Sep 29, 2013 at 20:53

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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'.
Glad it is helpful to you.
You forgot a close bracket on the if. But otherwise, this has helped me, thanks very much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.