我认为他们可以,但我没有把我的钱放在我的嘴(这么说)设置只读属性实际上似乎没有做任何事情。
我宁愿不使用Disabled,因为我希望选中的复选框与表单的其余部分一起提交,我只是不希望客户端能够在某些情况下更改它们。
我认为他们可以,但我没有把我的钱放在我的嘴(这么说)设置只读属性实际上似乎没有做任何事情。
我宁愿不使用Disabled,因为我希望选中的复选框与表单的其余部分一起提交,我只是不希望客户端能够在某些情况下更改它们。
当前回答
很晚才开始,但我找到了MVC的答案 我禁用了复选框,并在复选框之前添加了一个HiddenFor,因此当它发布时,如果首先找到隐藏字段并使用该值。这是可行的。
<div class="form-group">
@Html.LabelFor(model => model.Carrier.Exists, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.HiddenFor(model => model.Carrier.Exists)
@Html.CheckBoxFor(model => model.Carrier.Exists, new { @disabled = "disabled" })
@Html.ValidationMessageFor(model => model.Carrier.Exists)
</div>
</div>
其他回答
<input type="checkbox" onclick="this.checked=!this.checked;">
但你绝对必须验证服务器上的数据,以确保它没有被更改。
很晚才开始,但我找到了MVC的答案 我禁用了复选框,并在复选框之前添加了一个HiddenFor,因此当它发布时,如果首先找到隐藏字段并使用该值。这是可行的。
<div class="form-group">
@Html.LabelFor(model => model.Carrier.Exists, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.HiddenFor(model => model.Carrier.Exists)
@Html.CheckBoxFor(model => model.Carrier.Exists, new { @disabled = "disabled" })
@Html.ValidationMessageFor(model => model.Carrier.Exists)
</div>
</div>
这适用于我的Chrome浏览器:
<input type="checkbox" onclick="return false">
只读对<input type='checkbox'>无效
所以,如果你需要从表单中禁用的复选框中提交值,你可以使用jQuery:
$('form').submit(function(e) {
$('input[type="checkbox"]:disabled').each(function(e) {
$(this).removeAttr('disabled');
})
});
通过这种方式,在提交表单时从元素中删除禁用属性。
这是一个不能更改的复选框:
<input type="checkbox" disabled="disabled" checked="checked">
只需添加disabled="disabled"作为属性。
编辑以处理评论:
如果你想要数据被返回,那么一个简单的解决方案是将相同的名称应用到隐藏的输入:
<input name="myvalue" type="checkbox" disabled="disabled" checked="checked"/>
<input name="myvalue" type="hidden" value="true"/>
这样,当复选框被设置为“禁用”时,它只服务于数据的可视化表示,而不是实际“链接”到数据。在回发中,当复选框被禁用时,隐藏输入的值将被发送。