Can HTML checkboxes be set to readonly?

前端 未结 30 1627
無奈伤痛
無奈伤痛 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:07

    I used this to achieve the results:

    <input type=checkbox onclick="return false;" onkeydown="return false;" />
    
    0 讨论(0)
  • 2020-11-22 09:07

    If you want them to be submitted to the server with form but be not interacive for user, you can use pointer-events: none in css (works in all modern browsers except IE10- and Opera 12-) and set tab-index to -1 to prevent changing via keyboard. Also note that you can't use label tag as click on it will change the state anyway.

    input[type="checkbox"][readonly] {
      pointer-events: none !important;
    }
    
    td {
      min-width: 5em;
      text-align: center;
    }
    
    td:last-child {
      text-align: left;
    }
    <table>
      <tr>
        <th>usual
        <th>readonly
        <th>disabled
      </tr><tr>
        <td><input type=checkbox />
        <td><input type=checkbox readonly tabindex=-1 />
        <td><input type=checkbox disabled />
        <td>works
      </tr><tr>
        <td><input type=checkbox checked />
        <td><input type=checkbox readonly checked tabindex=-1 />
        <td><input type=checkbox disabled checked />
        <td>also works
      </tr><tr>
        <td><label><input type=checkbox checked /></label>
        <td><label><input type=checkbox readonly checked tabindex=-1 /></label>
        <td><label><input type=checkbox disabled checked /></label>
        <td>broken - don't use label tag
      </tr>
    </table>

    0 讨论(0)
  • 2020-11-22 09:08
    <input type="checkbox" onclick="this.checked=!this.checked;">
    

    But you absolutely MUST validate the data on the server to ensure it hasn't been changed.

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

    I happened to notice the solution given below. In found it my research for the same issue. I don't who had posted it but it wasn't made by me. It uses jQuery:

    $(document).ready(function() {
        $(":checkbox").bind("click", false);
    });
    

    This would make the checkboxes read only which would be helpful for showing readonly data to the client.

    0 讨论(0)
  • 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 '<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';
    }
    
    0 讨论(0)
  • 2020-11-22 09:13

    Just use simple disabled tag like this below.

    <input type="checkbox" name="email"  disabled>
    
    0 讨论(0)
提交回复
热议问题