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

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


当前回答

不,输入复选框不能为只读。

但是你可以用javascript把它们变成只读的!

通过防止用户以任何方式修改复选框,在任何时间的任何地方添加这段代码,使复选框像假设的那样为只读。

jQuery(document).on('click', function(e){ // check for type, avoid selecting the element for performance if(e.target.type == 'checkbox') { var el = jQuery(e.target); if(el.prop('readonly')) { // prevent it from changing state e.preventDefault(); } } }); input[type=checkbox][readonly] { cursor: not-allowed; } <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <label><input type="checkbox" checked readonly> I'm readonly!</label>

你可以在加载jQuery后的任何时候添加这个脚本。

它将适用于动态添加的元素。

它的工作原理是在页面上的任何元素上选择单击事件(发生在更改事件之前),然后检查该元素是否是只读复选框,如果是,则阻止更改。

为了不影响页面的性能,有太多的if。

其他回答

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

<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/

最新的jQuery有这个特性

$("#txtname").prop('readonly', false);
<input name="testName" type="checkbox" disabled>

@ (Model.IsEnabled) 使用此条件进行动态检查和取消检查,如果复选框已选中,则设置为只读。

 <input id="abc" name="abc"  type="checkbox" @(Model.IsEnabled ? "checked=checked onclick=this.checked=!this.checked;" : string.Empty) >

为什么不在上面用一个空白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;
}