我认为他们可以,但我没有把我的钱放在我的嘴(这么说)设置只读属性实际上似乎没有做任何事情。
我宁愿不使用Disabled,因为我希望选中的复选框与表单的其余部分一起提交,我只是不希望客户端能够在某些情况下更改它们。
我认为他们可以,但我没有把我的钱放在我的嘴(这么说)设置只读属性实际上似乎没有做任何事情。
我宁愿不使用Disabled,因为我希望选中的复选框与表单的其余部分一起提交,我只是不希望客户端能够在某些情况下更改它们。
当前回答
不,输入复选框不能为只读。
但是你可以用javascript把它们变成只读的!
通过防止用户以任何方式修改复选框,在任何时间的任何地方添加这段代码,使复选框像假设的那样为只读。
jQuery(document).on('click', function(e){ // check for type, avoid selecting the element for performance if(e.target.type == 'checkbox') { var el = jQuery(e.target); if(el.prop('readonly')) { // prevent it from changing state e.preventDefault(); } } }); input[type=checkbox][readonly] { cursor: not-allowed; } <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <label><input type="checkbox" checked readonly> I'm readonly!</label>
你可以在加载jQuery后的任何时候添加这个脚本。
它将适用于动态添加的元素。
它的工作原理是在页面上的任何元素上选择单击事件(发生在更改事件之前),然后检查该元素是否是只读复选框,如果是,则阻止更改。
为了不影响页面的性能,有太多的if。
其他回答
这适用于我的Chrome浏览器:
<input type="checkbox" onclick="return false">
READONLY对复选框不起作用,因为它阻止你编辑字段的值,但有了复选框,你实际上是在编辑字段的状态(在||关闭)
从faqs.org:
READONLY只是阻止用户更改字段的值,而不是阻止用户与字段交互,理解这一点很重要。例如,在复选框中,您可以打开或关闭复选框(从而设置CHECKED状态),但不能更改字段的值。
如果不想使用disabled,但仍然想提交值,那么当值不满足编辑条件时,将值作为隐藏字段提交并将其内容打印给用户如何?如。
// user allowed change
if($user_allowed_edit)
{
echo '<input type="checkbox" name="my_check"> Check value';
}
else
{
// Not allowed change - submit value..
echo '<input type="hidden" name="my_check" value="1" />';
// .. and show user the value being submitted
echo '<input type="checkbox" disabled readonly> Check value';
}
摘自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','');
}
我会对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。