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

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


当前回答

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

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

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

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

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

它的工作原理。

其他回答

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

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

<input name="testName" type="checkbox" disabled>

当将一个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。

人们喜欢只读复选框和(以及)只读无线电组的主要原因是,不能更改的信息可以以输入的形式显示给用户。

好的,禁用控件就可以做到这一点——不幸的是禁用控件不能用键盘导航,因此违反了所有可访问性法规。这是我所知道的HTML中最大的问题。

摘自https://stackoverflow.com/a/71086058/18183749

如果你不能使用'disabled'属性(因为它会擦除值的 input at POST),并注意到html属性'readonly'只工作 在文本区域和一些输入(文本,密码,搜索,据我所见), 最后,如果你不想重复你所有的 您可能会发现,带有隐藏输入逻辑的选择、复选框和单选 下面的函数或任何他的内部逻辑你喜欢:

    addReadOnlyToFormElements = function (idElement) {
        
            // html readonly don't work on input of type checkbox and radio, neither on select. So, a safe trick is to disable the non-selected items
            $('#' + idElement + ' input[type="checkbox"]:not(:checked)').prop('disabled',true); 
     
            // and, on the selected ones, to disable mouse/keyoard events and mimic readOnly appearance
            $('#' + idElement + ' input[type="checkbox"]:checked').prop('tabindex','-1').css('pointer-events','none').css('opacity','0.5');
        }

没有什么比删除这些只读更容易的了

removeReadOnlyFromFormElements = function (idElement) {

    // Remove the disabled attribut on non-selected
    $('#' + idElement + ' input[type="checkbox"]:not(:checked)').prop('disabled',false);

    // Restore mouse/keyboard events and remove readOnly appearance on selected ones
    $('#' + idElement + ' input[type="checkbox"]:checked').prop('tabindex','').css('pointer-events','').css('opacity','');
}