我认为他们可以,但我没有把我的钱放在我的嘴(这么说)设置只读属性实际上似乎没有做任何事情。
我宁愿不使用Disabled,因为我希望选中的复选框与表单的其余部分一起提交,我只是不希望客户端能够在某些情况下更改它们。
我认为他们可以,但我没有把我的钱放在我的嘴(这么说)设置只读属性实际上似乎没有做任何事情。
我宁愿不使用Disabled,因为我希望选中的复选框与表单的其余部分一起提交,我只是不希望客户端能够在某些情况下更改它们。
当前回答
<input name="isActive" id="isActive" type="checkbox" value="1" checked="checked" onclick="return false"/>
其他回答
这是一个不能更改的复选框:
<input type="checkbox" disabled="disabled" checked="checked">
只需添加disabled="disabled"作为属性。
编辑以处理评论:
如果你想要数据被返回,那么一个简单的解决方案是将相同的名称应用到隐藏的输入:
<input name="myvalue" type="checkbox" disabled="disabled" checked="checked"/>
<input name="myvalue" type="hidden" value="true"/>
这样,当复选框被设置为“禁用”时,它只服务于数据的可视化表示,而不是实际“链接”到数据。在回发中,当复选框被禁用时,隐藏输入的值将被发送。
这就出现了一些可用性问题。
如果您想要显示一个复选框,但不让它与之交互,那么为什么还要使用复选框呢?
然而,我的方法是使用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;
}
}
}
这里的一些答案似乎有点迂回,但这里有一个小技巧。
<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/
我会对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一直在寻找的。
如果你想让它们以表单的形式提交给服务器,但对用户来说不是交互式的,你可以在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>