zoukankan      html  css  js  c++  java
  • Can HTML checkboxes be set to readonly?

    Can HTML checkboxes be set to readonly?

    回答2

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

    回答3

    This is a checkbox you can't change:

    <input type="checkbox" disabled="disabled" checked="checked">
    

    Just add disabled="disabled" as an attribute.


    Edit to address the comments:

    If you want the data to be posted back, than a simple solutions is to apply the same name to a hidden input:

    <input name="myvalue" type="checkbox" disabled="disabled" checked="checked"/>
    <input name="myvalue" type="hidden" value="true"/>
    

    This way, when the checkbox is set to 'disabled', it only serves the purpose of a visual representation of the data, instead of actually being 'linked' to the data. In the post back, the value of the hidden input is being sent when the checkbox is disabled.

    评论

    Note that "disabled" checkbox doesn't send value via POST data.
    – biphobe
    Sep 20 '11 at 13:56
  • 相关阅读:
    异常测试之Socket网络异常
    关于文件的INode与Java中的文件操作接口
    java程序中获取kerberos登陆hadoop
    团队游戏的那些事
    细说内测
    如何搭建视频转码集群、播放服务器
    PropertyPlaceHolderConfigurer中的location是不是用错了?
    浅谈java classloader
    foreach写失效的问题
    ArcGIS中的WKID
  • 原文地址:https://www.cnblogs.com/chucklu/p/15714425.html
Copyright © 2011-2022 走看看