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

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


当前回答

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

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

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

其他回答

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

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

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

或者是:

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

迟来的回答,但大多数答案似乎过于复杂了。

据我所知,OP的基本要求是:

只读复选框显示状态。 随form返回的值。

应当指出的是:

The OP preferred not to use the disabled attribute, because they 'want the checked check boxes to be submitted with the rest of the form'. Unchecked checkboxes are not submitted with the form, as the quote from the OP in 1. above indicates they knew already. Basically, the value of the checkbox only exists if it is checked. A disabled checkbox clearly indicates that it cannot be changed, by design, so a user is unlikely to attempt to change it. The value of a checkbox is not limited to indicating its status, such as yes or false, but can be any text.

因此,由于readonly属性不起作用,不需要javascript的最佳解决方案是:

禁用的复选框,没有名称或值。 如果复选框要显示为已选中,则会显示存储在服务器上的具有名称和值的隐藏字段。

对于选中的复选框:

<输入类型=“复选框” 选中=“选中” 禁用=“禁用” /> <输入类型=“隐藏” 名称=“字段名称” 值=“字段值” />

对于未选中的复选框:

<输入类型=“复选框”禁用=“禁用” />

禁用输入的主要问题,特别是复选框,是它们的对比度差,这可能是一些视觉障碍的问题。用简单的词来表示值可能会更好,比如Status: none或Status: implemented,但在使用后者时包含上面的隐藏输入,比如:

<p>状态:执行<输入类型=“隐藏”

我会对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一直在寻找的。

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