设置复选框选中的值 [英] set unchecked value of checkbox

查看:146
本文介绍了设置复选框选中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有设定一个复选框的选中默认值的方法吗?
我不使用数据绑定控件。
这是从一种形式传递数据,并希望它传递false,而不是空的时候选中。

Is there a way of setting the default unchecked value of a checkbox? Am not using a databound control. This is passing data from a form and would like it to pass false as opposed to null when unchecked.

推荐答案

是,使用默认的 HtmlHelpers 将实现这个给你。

Yes, using the default HtmlHelpers will achieve this for you

<%: Html.Checkbox("myCheckbox") %>

或剃刀

@Html.Checkbox("myCheckbox")

复选框()方法将呈现一个输入类型=隐藏沿边领域输入类型=复选框将提交值当复选框被选中。

The Checkbox() method will render a input type="hidden" field along side the input type="checkbox" that will submit the value false when the checkbox is unchecked.

<input id="myCheckbox" name="myCheckbox" type="checkbox" value="true" />
<input name="myCheckbox" type="hidden" value="false" />

如果您要提交比以外的值false ,那么你应该呈现的复选框,并隐藏字段自己的隐藏字段的值设置为默认值。需要注意的是他们必须具有相同的名称属性。

If you want to submit a value other than false then you should render the checkbox and hidden field yourself setting the value of the hidden field to your default value. Note that they must have the same name attribute.

这篇关于设置复选框选中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆