Can HTML checkboxes be set to readonly?

前端 未结 30 2241
無奈伤痛
無奈伤痛 2020-11-22 08:39

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

30条回答
  •  清歌不尽
    2020-11-22 09:09

    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 ' Check value';
    }
    else
    {
        // Not allowed change - submit value..
        echo '';
        // .. and show user the value being submitted
        echo ' Check value';
    }
    

提交回复
热议问题