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

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


当前回答

我会使用readonly属性

<input type="checkbox" readonly>

然后使用CSS禁用交互:

input[type='checkbox'][readonly]{
    pointer-events: none;
}

注意,使用伪类:read-only在这里不起作用。

input[type='checkbox']:read-only{ /*not working*/
    pointer-events: none;
}

其他回答

我的解决方案实际上与FlySwat的解决方案相反,但我不确定它是否适用于您的情况。我有一组复选框,每个复选框都有一个提交表单的onClick处理程序(它们用于更改表的过滤器设置)。我不想允许多次单击,因为第一次单击之后的后续单击将被忽略。所以我禁用所有复选框后,第一次点击,并提交后的形式:

onclick = " document.forms [' form1’]。submit ();$('#filters input').each(function() {this。Disabled = true});"

复选框位于ID为“filters”的span元素中——代码的第二部分是一个jQuery语句,它遍历复选框并禁用每个复选框。这样,复选框值仍然通过表单提交(因为表单是在禁用它们之前提交的),并且它可以防止用户更改它们,直到页面重新加载。

如果其他人使用MVC和编辑器模板,这就是我如何控制显示只读属性(我使用自定义属性来获取If语句中的值)

@if (true)
{
    @Html.HiddenFor(m => m)
    @(ViewData.Model ? Html.Raw("Yes") : Html.Raw("No"))
} 
else
{               
    @Html.CheckBoxFor(m => m)
}

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

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

或者是:

$('your selector').click(function(evt){evt.preventDefault()});

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

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

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

它也适用于单选按钮。