Can HTML checkboxes be set to readonly?

前端 未结 30 1679
無奈伤痛
無奈伤痛 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 08:54

    I would have commented on ConroyP's answer, but that requires 50 reputation which I don't have. I do have enough reputation to post another answer. Sorry.

    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.

    Assume two boolean variables, $checked and $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" ' : '', '/>';
    

    The checkbox is displayed as checked if $checked is true. The checkbox is displayed as unchecked if $checked is false. The user can change the state of the checkbox if and only if $disabled is false. The "my_name" parameter is not posted when the checkbox is unchecked, by the user or not. The "my_name=1" parameter is posted when the checkbox is checked, by the user or not. I believe this is what Electrons_Ahoy was looking for.

    0 讨论(0)
  • 2020-11-22 08:54

    Contributing very very late...but anyway. On page load, use jquery to disable all checkboxes except the currently selected one. Then set the currently selected one as read only so it has a similar look as the disabled ones. User cannot change the value, and the selected value still submits.

    0 讨论(0)
  • 2020-11-22 08:55

    The main reason people would like a read-only check-box and (as well) a read-only radio-group is so that information that cannot be changed can be presented back to the user in the form it was entered.

    OK disabled will do this -- unfortunately disabled controls are not keyboard navigable and therefore fall foul of all accessibility legislation. This is the BIGGEST hangup in HTML that I know of.

    0 讨论(0)
  • 2020-11-22 08:57

    Belated answer, but most answers seem to over complicate it.

    As I understand it, the OP was basically wanting:

    1. Readonly checkbox to show status.
    2. Value returned with form.

    It should be noted that:

    1. The OP preferred not to use the disabled attribute, because they 'want the checked check boxes to be submitted with the rest of the form'.
    2. Unchecked checkboxes are not submitted with the form, as the quote from the OP in 1. above indicates they knew already. Basically, the value of the checkbox only exists if it is checked.
    3. A disabled checkbox clearly indicates that it cannot be changed, by design, so a user is unlikely to attempt to change it.
    4. The value of a checkbox is not limited to indicating its status, such as yes or false, but can be any text.

    Therefore, since the readonly attribute does not work, the best solution, requiring no javascript, is:

    1. A disabled checkbox, with no name or value.
    2. If the checkbox is to be displayed as checked, a hidden field with the name and value as stored on the server.

    So for a checked checkbox:

    <input type="checkbox" checked="checked" disabled="disabled" />
    <input type="hidden" name="fieldname" value="fieldvalue" />

    For an unchecked checkbox:

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

    The main problem with disabled inputs, especially checkboxes, is their poor contrast which may be a problem for some with certain visual disabilities. It may be better to indicate a value by plain words, such as Status: none or Status: implemented, but including the hidden input above when the latter is used, such as:

    <p>Status: Implemented<input type="hidden" name="status" value="implemented" /></p>

    0 讨论(0)
  • <input type="checkbox" onclick="return false" /> will work for you , I am using this

    0 讨论(0)
  • 2020-11-22 08:57

    Building on the above answers, if using jQuery, this may be an good solution for all inputs:

    <script>
        $(function () {
            $('.readonly input').attr('readonly', 'readonly');
            $('.readonly textarea').attr('readonly', 'readonly');
            $('.readonly input:checkbox').click(function(){return false;});
            $('.readonly input:checkbox').keydown(function () { return false; });
        });
    </script>
    

    I'm using this with Asp.Net MVC to set some form elements read only. The above works for text and check boxes by setting any parent container as .readonly such as the following scenarios:

    <div class="editor-field readonly">
        <input id="Date" name="Date" type="datetime" value="11/29/2012 4:01:06 PM" />
    </div>
    <fieldset class="flags-editor readonly">
         <input checked="checked" class="flags-editor" id="Flag1" name="Flags" type="checkbox" value="Flag1" />
    </fieldset>
    
    0 讨论(0)
提交回复
热议问题