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

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


当前回答

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

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

其他回答

onclick="javascript: return false;"

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

但是你可以用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。

这是一个不能更改的复选框:

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

这样,当复选框被设置为“禁用”时,它只服务于数据的可视化表示,而不是实际“链接”到数据。在回发中,当复选框被禁用时,隐藏输入的值将被发送。

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

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

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

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

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

它的工作原理。

在旧的HTML中可以使用

<input type="checkbox" disabled checked>text

但实际上不建议只使用旧的HTML,现在你应该使用XHTML。

在格式良好的XHTML中必须使用

<input type="checkbox" disabled="disabled" checked="checked" />text <!-- if yu have a checked box-->
<input type="checkbox" disabled="disabled" />text <!-- if you have a unchecked box -->

格式良好的XHTML需要XML表单,这就是使用disabled="disabled"而不是简单地使用disabled的原因。