Can HTML checkboxes be set to readonly?

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

    you can use this:

    <input type="checkbox" onclick="return false;"/>
    

    This works because returning false from the click event stops the chain of execution continuing.

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

    My solution is actually the opposite of FlySwat's solution, but I'm not sure if it will work for your situation. I have a group of checkboxes, and each has an onClick handler that submits the form (they're used for changing filter settings for a table). I don't want to allow multiple clicks, since subsequent clicks after the first are ignored. So I disable all checkboxes after the first click, and after submitting the form:

    onclick="document.forms['form1'].submit(); $('#filters input').each(function() {this.disabled = true});"

    The checkboxes are in a span element with an ID of "filters" - the second part of the code is a jQuery statement that iterates through the checkboxes and disables each one. This way, the checkbox values are still submitted via the form (since the form was submitted before disabling them), and it prevents the user from changing them until the page reloads.

    0 讨论(0)
  • 2020-11-22 08:51
    <input type="checkbox" readonly="readonly" name="..." />
    

    with jquery:

    $(':checkbox[readonly]').click(function(){
                return false;
            });
    

    it still might be a good idea to give some visual hint (css, text,...), that the control won't accept inputs.

    0 讨论(0)
  • 2020-11-22 08:51
    onclick="javascript: return false;"
    
    0 讨论(0)
  • 2020-11-22 08:51

    No, input checkboxes can't be readonly.

    But you can make them readonly with javascript!

    Add this code anywhere at any time to make checkboxes readonly work as assumed, by preventing the user from modifying it in any way.

    jQuery(document).on('click', function(e){
          // check for type, avoid selecting the element for performance
          if(e.target.type == 'checkbox') {
              var el = jQuery(e.target);
              if(el.prop('readonly')) {
                  // prevent it from changing state
                  e.preventDefault();
              }
          }
    });
    input[type=checkbox][readonly] {
        cursor: not-allowed;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <label><input type="checkbox" checked readonly> I'm readonly!</label>

    You can add this script at any time after jQuery has loaded.

    It will work for dynamically added elements.

    It works by picking up the click event (that happens before the change event) on any element on the page, it then checks if this element is a readonly checkbox, and if it is, then it blocks the change.

    There are so many ifs to make it not affect the performance of the page.

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

    another "simple solution":

    <!-- field that holds the data -->
    <input type="hidden" name="my_name" value="1" /> 
    <!-- visual dummy for the user -->
    <input type="checkbox" name="my_name_visual_dummy" value="1" checked="checked" disabled="disabled" />
    

    disabled="disabled" / disabled=true

    0 讨论(0)
提交回复
热议问题