ASP.NET MVC submitting form on checkbox click

2019-05-26 19:34发布

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条回答
The star\"
2楼-- · 2019-05-26 20:01

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.

查看更多
登录 后发表回答