我认为他们可以,但我没有把我的钱放在我的嘴(这么说)设置只读属性实际上似乎没有做任何事情。
我宁愿不使用Disabled,因为我希望选中的复选框与表单的其余部分一起提交,我只是不希望客户端能够在某些情况下更改它们。
我认为他们可以,但我没有把我的钱放在我的嘴(这么说)设置只读属性实际上似乎没有做任何事情。
我宁愿不使用Disabled,因为我希望选中的复选框与表单的其余部分一起提交,我只是不希望客户端能够在某些情况下更改它们。
当前回答
如果你想要你所有的复选框都是“锁定”的,这样用户就不能改变“checked”状态,如果“readonly”属性是存在的,那么你可以使用jQuery:
$(':checkbox').click(function () {
if (typeof ($(this).attr('readonly')) != "undefined") {
return false;
}
});
这段代码很酷的一点是,它允许您在整个代码中更改“readonly”属性,而不必重新绑定每个复选框。
它也适用于单选按钮。
其他回答
在旧的HTML中可以使用
<input type="checkbox" disabled checked>text
但实际上不建议只使用旧的HTML,现在你应该使用XHTML。
在格式良好的XHTML中必须使用
<input type="checkbox" disabled="disabled" checked="checked" />text <!-- if yu have a checked box-->
<input type="checkbox" disabled="disabled" />text <!-- if you have a unchecked box -->
格式良好的XHTML需要XML表单,这就是使用disabled="disabled"而不是简单地使用disabled的原因。
在这里,你们没有人想到的是,你们都在使用JavaScript,任何用户都可以很容易地绕过它。
简单地禁用它将禁用所有JQuery/Return False语句。
只读复选框的唯一选项是服务器端。
显示他们,让他们改变他们,但不接受新的帖子数据从他们。
迟来的回答,但大多数答案似乎过于复杂了。
据我所知,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>状态:执行<输入类型=“隐藏”
我知道“disabled”不是一个可接受的答案,因为操作希望它发布。但是,即使设置了只读选项集,也必须在服务器端验证值。这是因为您无法阻止恶意用户使用readonly属性发布值。
我建议存储原始值(服务器端),并将其设置为禁用。然后,当他们提交表单时,忽略发布的任何值,并使用您存储的原始值。
它的外观和行为都像一个只读值。它处理(忽略)来自恶意用户的帖子。你真是一举两得。
如果你想让它们以表单的形式提交给服务器,但对用户来说不是交互式的,你可以在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>