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

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


当前回答

在旧的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的原因。

其他回答

<input type="checkbox" onclick="this.checked=!this.checked;">

但你绝对必须验证服务器上的数据,以确保它没有被更改。

在旧的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的原因。

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

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

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

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

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

我只是不希望客户在某些情况下能够更改它们。

READONLY本身不起作用。你可能可以用CSS做一些奇怪的事情,但我们通常只是禁用它们。

警告:如果它们被发布回来,那么客户端可以更改它们,句号。你不能依靠只读来阻止用户修改某些东西。总是可以使用fiddler或只是改变html w/firebug或诸如此类的东西。