我认为他们可以,但我没有把我的钱放在我的嘴(这么说)设置只读属性实际上似乎没有做任何事情。

我宁愿不使用Disabled,因为我希望选中的复选框与表单的其余部分一起提交,我只是不希望客户端能够在某些情况下更改它们。


当前回答

目前的大多数答案都有以下一个或多个问题:

只检查鼠标而不是键盘。 只检查页面加载。 挂钩总是流行的变更或提交事件,如果有其他东西挂钩它们,它们就不会总是有效。 需要一个隐藏的输入或其他特殊的元素/属性,你必须撤销,以便重新启用复选框使用javascript。

下面的方法很简单,没有这些问题。

$('input[type="checkbox"]').on('click keyup keypress keydown', function (event) {
    if($(this).is('[readonly]')) { return false; }
});

如果复选框是只读的,它不会改变。如果不是,也会是。它确实使用了jquery,但你可能已经在使用它了…

它的工作原理。

其他回答

这里的一些答案似乎有点迂回,但这里有一个小技巧。

<form id="aform" name="aform" method="POST">
    <input name="chkBox_1" type="checkbox" checked value="1" disabled="disabled" />
    <input id="submitBttn" type="button" value="Submit" onClick='return submitPage();'>
</form>​

然后在jquery中,你可以选择以下两个选项之一:

$(document).ready(function(){
    //first option, you don't need the disabled attribute, this will prevent
    //the user from changing the checkbox values
    $("input[name^='chkBox_1']").click(function(e){
        e.preventDefault();
    });

    //second option, keep the disabled attribute, and disable it upon submit
    $("#submitBttn").click(function(){
        $("input[name^='chkBox_1']").attr("disabled",false);
        $("#aform").submit();
    });

});

演示: http://jsfiddle.net/5WFYt/

很晚才开始,但我找到了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>

不,但是您可以使用javascript事件来实现类似的功能

我用这个方法得到了如下结果:

<input type=checkbox onclick="return false;" onkeydown="return false;" />

为什么不在上面用一个空白div覆盖输入呢?

<div id='checkbox_wrap'>
   <div class='click_block'></div>
   <input type='checkbox' /> 
</div>

然后是CSS

 #checkbox_wrap{
   height:10px;
   width:10px;
   }
 .click_block{
   height: 10px;
   width: 10px;
   position: absolute;
   z-index: 100;
}