【问题标题】:Why does a checkbox also have a hidden tag with it? [duplicate]为什么复选框也带有隐藏标签? [复制]
【发布时间】:2011-06-03 15:16:27
【问题描述】:

可能重复:
asp.net mvc: why is Html.CheckBox generating an additional hidden input

我在一个 asp.net mvc 应用程序中呈现一个复选框,并且该控件还输出一个隐藏字段,如下所示:

<input id="blah-64" name="blah-64" value="true" type="checkbox">
<input name="blah-64" value="false" type="hidden">

问题是,当我发布表单时,表单键“blah-64”返回“开、关”。

这是为什么?

【问题讨论】:

    标签: asp.net html asp.net-mvc checkbox


    【解决方案1】:

    如果您查看 Checkbox 帮助器的源代码,您会看到解释它的注释。它看起来像这样:

    if (inputType == InputType.CheckBox) { 
        // Render an additional <input type="hidden".../> for checkboxes. This 
        // addresses scenarios where unchecked checkboxes are not sent in the request. 
        // Sending a hidden input makes it possible to know that the checkbox was present 
        // on the page when the request was submitted. 
    ...
    

    这是因为W3C specification 说,“提交表单时,只有“打开”复选框控件才能成功。” (成功是他们提交的价值的期限)。隐藏字段确保它们被提交,无论它们是否被选中。如果您不想要这种行为,那么最简单的方法是自己渲染 HTML,不要使用帮助器。

    【讨论】:

    • 但我该取哪个值?如果我检查它,我会“打开,关闭”??
    猜你喜欢
    • 2018-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-09
    • 2017-07-08
    相关资源
    最近更新 更多