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

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


当前回答

你总是可以使用一个小图像,看起来像一个复选框。

其他回答

在这里,你们没有人想到的是,你们都在使用JavaScript,任何用户都可以很容易地绕过它。

简单地禁用它将禁用所有JQuery/Return False语句。

只读复选框的唯一选项是服务器端。

显示他们,让他们改变他们,但不接受新的帖子数据从他们。

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

这就出现了一些可用性问题。

如果您想要显示一个复选框,但不让它与之交互,那么为什么还要使用复选框呢?

然而,我的方法是使用disabled(用户希望禁用的复选框不可编辑,而不是使用JS使启用的复选框无法工作),并使用javascript添加一个表单提交处理程序,在表单提交之前启用复选框。这样你就能把你的价值观公布出来。

是这样的:

var form = document.getElementById('yourform');
form.onSubmit = function () 
{ 
    var formElems = document.getElementsByTagName('INPUT');
    for (var i = 0; i , formElems.length; i++)
    {  
       if (formElems[i].type == 'checkbox')
       { 
          formElems[i].disabled = false;
       }
    }
}

只读对<input type='checkbox'>无效

所以,如果你需要从表单中禁用的复选框中提交值,你可以使用jQuery:

$('form').submit(function(e) {
    $('input[type="checkbox"]:disabled').each(function(e) {
        $(this).removeAttr('disabled');
    })
});

通过这种方式,在提交表单时从元素中删除禁用属性。

我会对ConroyP的回答进行评论,但这需要50个声望,而我没有。我确实有足够的声誉来发布另一个答案。对不起。

The problem with ConroyP's answer is that the checkbox is rendered unchangeable by not even including it on the page. Although Electrons_Ahoy does not stipulate as much, the best answer would be one in which the unchangeable checkbox would look similar, if not the same as, the changeable checkbox, as is the case when the "disabled" attribute is applied. A solution which addresses the two reasons Electrons_Ahoy gives for not wanting to use the "disabled" attribute would not necessarily be invalid because it utilized the "disabled" attribute.

假设有两个布尔变量$checked和$disabled:

if ($checked && $disabled)
    echo '<input type="hidden" name="my_name" value="1" />';
echo '<input type="checkbox" name="my_name" value="1" ',
    $checked ? 'checked="checked" ' : '',
    $disabled ? 'disabled="disabled" ' : '', '/>';

如果$checked为true,则复选框显示为选中。如果$checked为false,则该复选框显示为未选中。当且仅当$disabled为false时,用户可以更改复选框的状态。当复选框未选中时,不管用户是否选中,“my_name”参数都不会发布。“my_name=1”参数在复选框被选中时被发布,无论用户是否选中。我相信这就是Electrons_Ahoy一直在寻找的。