如何实现输入复选框的勾选动作?

How to implement input checkbox's checked action?

在 WebForm 中,我有一个 input checkbox,我想对其应用服务器端操作。 例如,选中复选框时,我想更改一些标签的文本。 我尝试在客户端使用:

<input id="auto" name="auto" type="checkbox" data-toggle="toggle" data-on="AUTOMAT" data-off="MANUAL" <%= string.IsNullOrEmpty(Request["auto"]) ? string.Empty : "checked" %> />
<asp:Label ID="Label3" runat="server" Text="Label"></asp:Label>   
<asp:HiddenField ID="customSwitch1Change" runat="server" Value="0" />
        <script>
            $('#auto').click(function () {
                $('#<%=customSwitch1Change.ClientID%>').val("1");
                $('#form1').submit();
            });
        </script>

我使用 this and this 作为输入复选框。

在服务器端我试过:

protected void CustomSwitch1Change(string auto)
{

    if (string.IsNullOrEmpty(auto))
    {
        Label3.Text = $"customSwitch1 was not checked.";
    }
    else
    {
        Label3.Text = $"customSwitch1 was checked and the check value is {auto}.";
    }
}

但是我试过的方法没有用。 我做错了什么?或者还有其他方法吗?

您不需要 HiddenField。如果您将 jQuery 更改为下面的代码,它将在 CheckBox 更改时执行表单 post。

<script>
    $('#auto').change(function () {
        $('#form1').submit();
    });
</script>

然后你可以简单地用

获取代码后面的值
string auto = Request.Form["auto"];