可以将HTML复选框设置为只读吗? [英] Can HTML checkboxes be set to readonly?

查看:103
本文介绍了可以将HTML复选框设置为只读吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为他们可以,但因为我不把我的钱在我的嘴(可以说)设置readonly属性实际上似乎并不做任何事情。



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

解决方案

READONLY 不能用于复选框,因为它阻止您编辑字段的,但带有一个复选框,您实际上正在编辑字段的状态(在|| off上)



<从 faqs.org


重要的是要理解READONLY只是阻止用户更改字段的值,而不是与字段交互。例如,在复选框中,您可以打开或关闭(因此设置CHECKED状态),但不更改字段的值。


如果你不想使用 disabled ,但仍然要提交值,如何将值提交为隐藏字段,并只打印其内容用户何时不满足编辑标准?例如

  //用户允许更改
if($ user_allowed_edit)
{
echo' < input type =checkboxname =my_check>检查值';
}
else
{
//不允许更改 - 提交值..
echo'< input type =hiddenname =my_checkvalue = 1/>';
// ..并显示用户提交的值
echo'< input type =checkboxdisabled readonly>检查值';
}


I thought they could be, but as I'm not putting my money where my mouth was (so to speak) setting the readonly attribute doesn't actually seem to do anything.

I'd rather not use Disabled, since I want the checked check boxes to be submitted with the rest of the form, I just don't want the client to be able to change them under certain circumstances.

解决方案

READONLY doesn't work on checkboxes as it prevents you from editing a field's value, but with a checkbox you're actually editing the field's state (on || off)

From faqs.org:

It's important to understand that READONLY merely prevents the user from changing the value of the field, not from interacting with the field. In checkboxes, for example, you can check them on or off (thus setting the CHECKED state) but you don't change the value of the field.

If you don't want to use disabled but still want to submit the value, how about submitting the value as a hidden field and just printing its contents to the user when they don't meet the edit criteria? e.g.

// 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';
}

这篇关于可以将HTML复选框设置为只读吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆