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

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


当前回答

如果你想要你所有的复选框都是“锁定”的,这样用户就不能改变“checked”状态,如果“readonly”属性是存在的,那么你可以使用jQuery:

$(':checkbox').click(function () {
    if (typeof ($(this).attr('readonly')) != "undefined") {
        return false;
    }
});

这段代码很酷的一点是,它允许您在整个代码中更改“readonly”属性,而不必重新绑定每个复选框。

它也适用于单选按钮。

其他回答

当将一个HTML复选框发布到服务器时,它有一个字符串值'on'或''。

Readonly不会停止用户编辑复选框,disabled会停止返回的值。 解决这个问题的一种方法是使用一个隐藏元素来存储实际值,而显示的复选框是一个禁用的dummy。通过这种方式,复选框状态在帖子之间被持久化。

这是一个函数。它使用'T'或'F'的字符串,你可以以任何你喜欢的方式更改它。这是在一个ASP页面中使用的使用服务器端的VB脚本。

public function MakeDummyReadonlyCheckbox(i_strName, i_strChecked_TorF)

    dim strThisCheckedValue

    if (i_strChecked_TorF = "T") then
        strThisCheckedValue = " checked "
        i_strChecked_TorF = "on"
    else
        strThisCheckedValue = ""
        i_strChecked_TorF = ""
    end if

    MakeDummyReadonlyCheckbox = "<input type='hidden' id='" & i_strName & "' name='" & i_strName & "' " & _
        "value='" & i_strChecked_TorF & "'>" & _
    "<input type='checkbox' disabled id='" & i_strName & "Dummy' name='" & i_strName & "Dummy' " & _
        strThisCheckedValue & ">"   
end function

public function GetCheckbox(i_objCheckbox)

    select case trim(i_objCheckbox)

        case ""
            GetCheckbox = "F"

        case else
            GetCheckbox = "T"

    end select

end function

在ASP页面的顶部,你可以提取持久值…

strDataValue = GetCheckbox(Request.Form("chkTest"))

当你想输出你的复选框时,你可以这样做…

response.write MakeDummyReadonlyCheckbox("chkTest", strDataValue)

我已经测试过了,它工作得很好。它也不依赖于JavaScript。

我碰巧注意到下面给出的解决方案。在我的研究中发现了同样的问题。 我不知道是谁发的,但不是我做的。它使用jQuery:

$(document).ready(function() {
    $(":checkbox").bind("click", false);
});

这将使复选框为只读,这将有助于向客户端显示只读数据。

如果你想让它们以表单的形式提交给服务器,但对用户来说不是交互式的,你可以在css中使用pointer-events: none(适用于所有现代浏览器,除了IE10-和Opera 12-),并将tab-index设置为-1以防止通过键盘更改。还要注意,你不能使用标签标签,因为点击它会改变状态。

input[type="checkbox"][readonly] { pointer-events: none !important; } td { min-width: 5em; text-align: center; } td:last-child { text-align: left; } <table> <tr> <th>usual <th>readonly <th>disabled </tr><tr> <td><input type=checkbox /> <td><input type=checkbox readonly tabindex=-1 /> <td><input type=checkbox disabled /> <td>works </tr><tr> <td><input type=checkbox checked /> <td><input type=checkbox readonly checked tabindex=-1 /> <td><input type=checkbox disabled checked /> <td>also works </tr><tr> <td><label><input type=checkbox checked /></label> <td><label><input type=checkbox readonly checked tabindex=-1 /></label> <td><label><input type=checkbox disabled checked /></label> <td>broken - don't use label tag </tr> </table>

另一个“简单的解决方案”:

<!-- field that holds the data -->
<input type="hidden" name="my_name" value="1" /> 
<!-- visual dummy for the user -->
<input type="checkbox" name="my_name_visual_dummy" value="1" checked="checked" disabled="disabled" />

禁用=“禁用” / 禁用=真

<input type="checkbox" readonly="readonly" name="..." />

jquery:

$(':checkbox[readonly]').click(function(){
            return false;
        });

它仍然可能是一个好主意,给一些视觉提示(css,文本,…),控件将不接受输入。