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
  • 相关阅读:
    java_db2错误码对应值
    oracle_用户与概要文件
    quartz配置时间
    bzoj2395: [Balkan 2011]Timeismoney
    bzoj2725: [Violet 6]故乡的梦
    bzoj4400: tjoi2012 桥
    双连通分量模板
    bzoj3047: Freda的传呼机 && 2125: 最短路
    bzoj3541: Spoj59 Bytelandian Information Agency
    bzoj1023: [SHOI2008]cactus仙人掌图
  • 原文地址:https://www.cnblogs.com/chucklu/p/15714425.html
Copyright © 2011-2022 走看看